0

使用 Child 进行 XML 解析而不是值解析

   import java.io.File;
   import java.io.FileInputStream;

   import javax.xml.xpath.XPath;
   import javax.xml.xpath.XPathConstants;
   import javax.xml.xpath.XPathFactory;

   import org.w3c.dom.Node;
   import org.w3c.dom.NodeList;
   import org.xml.sax.InputSource;

   import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList;

    public class XPathEvaluator {
    /*
   * ServiceGroup serviceGroup = new ServiceGroup(); List<Service>
    * requiredServices = new ArrayList<Service>(); List<Service>
  * recommandedServices = new ArrayList<Service>(); Service service = new
  * Service();
  */

public void evaluateDocument(File xmlDocument) {

    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        String requiredServicesExpression = "/Envelope/Header";
        InputSource requiredServicesInputSource = new InputSource(
                new FileInputStream(xmlDocument));
        DTMNodeList requiredServicesNodes = (DTMNodeList) xPath.evaluate(
                requiredServicesExpression, requiredServicesInputSource,
                XPathConstants.NODESET);
        System.out.println(requiredServicesNodes.getLength());
        NodeList requiredNodeList = (NodeList) requiredServicesNodes;

        for (int i = 0; i < requiredNodeList.getLength(); i++) {
            Node node = requiredNodeList.item(i);
            System.out.println(node.getChildNodes());

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] argv) {
    XPathEvaluator evaluator = new XPathEvaluator();
    File xmlDocument = new File("d://eva.xml");
    evaluator.evaluateDocument(xmlDocument);

 }

}

我的 xml 在此我正在尝试解析标头信息

   <?xml version="1.0" encoding="UTF-8"?>
   <Envelope>
       <Header>
            <User id="MAKRISH"/>
            <Request-Id id="1"/>
            <Type name="Response"/>
            <Application-Source name="vss" version="1.0"/>
            <Application-Destination name="test" />
            <Outgo-Timestamp date="2012-08-24" time="14:50:00"/>
            <DealerCode>08301</DealerCode>
            <Market>00000</Market>
        </Header>
   </Envelope>

我无法获得 Header child 我如何才能获得它们它在 getchildNodes 方法上给了我 null 。我检查了很多解决方案,但得到了任何东西。

4

2 回答 2

1

以下解析是根据标记使用 DOM 完成的,我希望这可以帮助您解决

{

  try{

            File file = new File("xmlfile");
            DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(file);
            Element root = document.getDocumentElement();
            root.normalize();
            printNode(root, 0);
             } catch (Exception e) {
                  }
            }
            public static void printNode(Node node, int depth) {
            if (node.getNodeType() == Node.TEXT_NODE) {
            System.out.printf("%s%n",  node.getNodeValue());
            } else {
            NamedNodeMap attributes = node.getAttributes();
            if ((attributes == null) || (attributes.getLength() == 0)) {
            System.out.printf("%s%n", node.getNodeName());
            } else {

            System.out.printf("%s ", node.getNodeName());
        printAttributes(attributes);
        }
       }
        NodeList children = node.getChildNodes();

        for(int i=0; i<children.getLength(); i++) {

        Node childNode = children.item(i);

        printNode(childNode, depth+1);

        }
           }
       private static void printAttributes(NamedNodeMap attributes) {
       for(int i=0; i<attributes.getLength(); i++) 
    {
       Node attribute = attributes.item(i);
       System.out.printf(" %s=\"%s\"", attribute.getNodeName(),
       attribute.getNodeValue());       
   }                                                                                     

 }                                                                                          
}
于 2012-08-27T11:38:22.593 回答
0

这个相关问题的公认答案是使用 xpath 解析 xml 的一个很好的例子。

我已经调试了你的代码,getChildNodes 调用实际上没有返回 null,但它有一个令人困惑的toString().

于 2012-08-27T12:19:09.237 回答