0

META次の XML を使用して、この 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);
          }
        }

しかし、私は何の出力も得ていません。私に何ができる?

4

1 に答える 1

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;
    }

さらに質問がある場合はお知らせください

于 2012-12-27T07:41:59.880 に答える