how to read a specific element from multi level tags of xml(DOM) by using java

房东的猫 提交于 2019-12-13 04:06:02

问题


I want to read a multi level tags from xml(DOM) using java and the sample xml is :

<root>
     <subclass>
         <subclass>
             <subclass>
                 <name>test1</name>
                 <address>address1</address>
              </subclass>
               <name>test2</name>
               <address>address2</address>
         </subclass>
         <name>test3</name>
         <address>address3</address>
     </subclass>
 </root> 
How to read <name>test2</name> and <address>address2</address> from the above xml?

I have given a sample code .. but i need to find the values dynamically. when i am iterating bu using subclass tag, it's giving all the data. just i want to know how to get the specific data like <name>test2</name> and <address>address2</address> .

Below is my java code which is reading the above xml:

NodeList fList = firstWordElement
                        .getElementsByTagName("root");
                for (int i = 0; i < fList.getLength(); i++) {
                    Node firstFLNode = fList.item(i);
                    if (firstFLNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element firstWdElement = (Element) firstFLNode;
                        NodeList firstWdList = firstWdElement.getElementsByTagName("innerclass");

                        for (int j = 0; j < firstWdList.getLength(); j++) {

                            Element firstWd1Element = (Element) firstWdList.item(j);

                            if (firstWd1Element.getNodeType() == Node.ELEMENT_NODE) {
                                String InnerName = ParseUtil.getTagValue("name", firstWd1Element);
                                String InnerFormat = ParseUtil.getTagValue("format", firstWd1Element);
                                String InnerDescription = ParseUtil.getTagValue("description", firstWd1Element);

                                NodeList innerClassList = firstWd1Element.getElementsByTagName("subclass");

                                for (int k = 0; k < innerClassList.getLength(); k++) {

                                    Element subClassElement = (Element) innerClassList
                                            .item(k);
                                    if (subClassElement.getNodeType() == Node.ELEMENT_NODE) {
                                        String InnerSubName = ParseUtil.getTagValue("name", subClassElement);
                                        System.out.println("Innername==="+ InnerSubName);
                                        String InnerSubFormat = ParseUtil.getTagValue("format", subClassElement);
                                        System.out.println("Innerformat==="+ InnerSubFormat);
                                        String InnerSubDescription = ParseUtil.getTagValue("description", subClassElement);
                                        System.out.println("Innerdescription==="+ InnerSubDescription);

                                    }
                                }

                            }



                        }
                    }
                }

回答1:


A quick way to do this is by using XPath queries. Check out these tutorials:

http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html

http://www.javabeat.net/2009/03/how-to-query-xml-using-xpath/




回答2:


Traditionally you have to iterate trough the nodes, usually done by creating a NodeList with getChildren() from the parent node. If you only want the test2 -node you have to perform some sort of comparison - you can't jump straight to the second node. That is to say you can, but that wont make a very robust or scale able solution.



来源:https://stackoverflow.com/questions/12641246/how-to-read-a-specific-element-from-multi-level-tags-of-xmldom-by-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!