我们正在尝试使用以下XML
获取此META
TAG的数据
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/12/soap-envelope MIAL_SOAP_AIDX_Flight_Msg.xsd" xmlns:iata="http://www.iata.org/IATA/2007/00" xmlns:p="http://www.ibm.com/xmlns/transportation/airport/meta" xmlns:soap="http://www.w3.org/2001/12/soap-envelope" encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
<p:Meta>
<p:SNDR>AODB</p:SNDR>
<p:TMST>2011-01-10T11:00:00</p:TMST>
<p:SEQN>25</p:SEQN>
<p:TYPE>FLSH</p:TYPE>
<p:SUBT>DALY</p:SUBT>
</p:Meta>
</soap:Header>
代码是:
public List<Item> readConfig(String configFile) {
List<Item> items = new ArrayList<Item>();
try {
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = new FileInputStream(configFile);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// Read the XML document
Item item = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
System.out.println("in while:::");
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
//added
if (startElement.getName().getLocalPart() == (Meta)) {
item = new Item();
if (event.isStartElement()) {
if (event.asStartElement().getName().getLocalPart()
.equals(SUBT)) {
event = eventReader.nextEvent();
item.setSubt(event.asCharacters().getData());
continue;
}
if (event.isEndElement()) {
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart() == (Meta)) {
items.add(item);
}
}
但我没有得到任何输出。我该怎么办?
答案 0 :(得分:0)
首先阅读您的XMl并返回文档
/**
* This method reads in the xmlFile, validates it against the
* schemaFile, and if valid, loads it into a WhitespaceFreeXMLDoc
* and returns it, which helps because that's a much easier
* format for us to deal with.
*
* @param xmlFile Path and name of xml file to load.
*
* @param schemaFile Path and name of schema file to use for validation.
*
* @return A normalized Document object fully loaded with the data found
* in the xmlFile.
*
* @throws InvalidXMLFileFormatException Thrown if the xml file validation fails.
*/
public Document loadXMLDocument(String xmlFile, String xsdFile)
throws InvalidXMLFileFormatException
{
// FIRST VALIDATE
boolean isValid = validateXMLDoc(xmlFile, xsdFile);
if (!isValid)
{
throw new InvalidXMLFileFormatException(xmlFile, xsdFile);
}
// THIS IS JAVA API STUFF
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
// FIRST RETRIEVE AND LOAD THE FILE INTO A TREE
DocumentBuilder db = dbf.newDocumentBuilder();
Document xmlDoc = db.parse(xmlFile);
xmlDoc.getDocumentElement().normalize();
// LET'S RETURN THE DOC
return xmlDoc;
}
// THESE ARE XML-RELATED ERRORS THAT COULD HAPPEN DURING
// LOADING AND PARSING IF THE XML FILE IS NOT WELL FORMED
// OR IS NOW WHERE AND WHAT WE SAY IT IS
catch(ParserConfigurationException | SAXException | IOException pce)
{
throw new InvalidXMLFileFormatException(xmlFile);
}
}
然后通过传递其名称
来获取META节点/**
* This method can be used to get the node in the document
* that is an element of type tagName. null is returned
* if none is found.
*
* @param doc The XML document to search
*
* @param tagName The name of the XML element/tag to
* search for.
*
* @return The first node found named tagName. If none is
* found in the document, null is returned.
*/
public Node getNodeWithName(Document doc, String tagName)
{
// GET THE NODE FOR THE tagName ELEMENT
NodeList nodeList = doc.getElementsByTagName(tagName);
// IF NOT FOUND, DON'T GO ON
if (nodeList.getLength() == 0)
{
return null;
}
// IT WAS FOUND, SO GET THE DATA
Node node = nodeList.item(0);
return node;
}
如果您还有其他问题,请与我们联系