我如何在xml中获取路径

时间:2015-11-01 13:34:59

标签: java xml

我有像这样的结构的XML。

<?xml version="1.0" encoding="UTF-8" ?>
 <root>
   <child1 name="1"/>
   <child2 name="2"/>
   <child3 name="3">
          <condition>x>=50</condition>
          <childofchild3 name="3.1">
             <condition>y<40</condition>
                <childOfchild3.1 name="3.1.1">
                   <condition>a>70</condition>
                      <step>
                        <a1>
                          <aa1> </aa1>
                        </a1>   
                        <b1 />
                      </step>

                     <c1>
                       <a1>
                         <aa1> </aa1>
                       </a1>    
                     </c1>
             </childOfchild3.1>
            <c1>
              <a1>
                <aa1> </aa1>
              </a1> 
            </c1>
          </childOfchild3>
          <c1>
             <a1>
                <aa1> </aa1>
             </a1>  
         </c1>
   </child3>
   <child4 name="4" />
 </root>

我必须使用java打印带有值的节点路径。以下是我必须得到的输出样本:

child1 -> child2 -> child3 -> childOfchild3 -> chileOfchild3.1 -> a1 -> b1  -> child4
child1 -> child2 -> child3 -> childOfchild3 -> chileOfchild3.1 -> c1 -> a1-> child4
child1 -> child2 -> child3 -> childOfchild3 -> c1 -> a1-> child4
child1 -> child2 -> child3 -> c1 -> a1-> child4

这是最后一个java代码,但它无法正常工作

public class xmlPath{
static List <String []> valueList = new ArrayList<String []>() ;
static String rootPath = "";
    public static void showPath (){
        try{
        File xml = new File("test.xml");

        String nextNode="";
        DocumentBuilderFactory   dbFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xml);
        Element root = doc.getDocumentElement();
        rootPath += "root";
        NodeList sequenceList = doc.getElementsByTagName("root");
        Element sequenceNode = (Element) sequenceList.item(0);
        NodeList sequenceChildList =  sequenceNode.getChildNodes();

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

             if(sequenceChildList.item(i) instanceof Element && sequenceChildList.item(i).getNodeType() == Node.ELEMENT_NODE){

                 nextNode = rootPath+" > "+ sequenceChildList.item(i).getNodeName(); 

                    if(sequenceChildList.item(i).hasChildNodes()){
                        findPath(sequenceChildList.item(i).getChildNodes(), nextNode);
                    }
                    else{
                      if(sequenceChildList.item(i).hasAttributes()){        
                             NamedNodeMap nameAttr = sequenceChildList.item(i).getAttributes();
                              for(int j=0;j<nameAttr.getLength();j++){
                                  Node node = nameAttr.item(j);
                                 if ("name".equals(node.getNodeName())) {
                                    valueList.add(new String []{nextNode,node.getNodeValue()}); 

                                    }
                              }
                         } 


                    }
             }
        }
        Iterator <String []> iT = valueList.iterator();
        for(;iT.hasNext();){
            String [] val = iT.next();
            if(!val[0].contains("condition")){
            System.out.println(val[0]+" = "+val[1]);
          }
        }
        } catch(Exception e ){
            e.printStackTrace();
        }
    }
    public static void findPath(NodeList seQuenceList,String listPath){
        String path, value,count,name=null;
        Node nextNode;
        for(int i=0;i<seQuenceList.getLength();i++){


            if(seQuenceList.item(i) instanceof Element){

                path = listPath+" > "+seQuenceList.item(i).getNodeName();


                    if(seQuenceList.item(i).getChildNodes().getLength() >=1){

                        findPath(seQuenceList.item(i).getChildNodes(), path);
                    }

                    else if(!seQuenceList.item(i).hasChildNodes()){
                        nextNode= nextNode(seQuenceList.item(i));
                            if(nextNode !=null){
                                findPath(nextNode.getChildNodes(), listPath);
                            }
                            if(nextNode ==null){

                                System.out.println("Not chileNode");
                                valueList.add(new String []{listPath,name});
                            }  

                    }                               
            }
        } 
    }
    public static Node nextNode(Node currentNode){
            Node nextNode = currentNode.getNextSibling();
            if(currentNode.getNodeType() ==Node.ELEMENT_NODE){
                while(!(nextNode instanceof Element) && nextNode != null){
                    nextNode = nextNode.getNextSibling();
                }
            }
            return nextNode;
    }

这是实际输出。

child1
child2
child3 -> childOfchild3 -> childOfchild3.1 -> a1 -> aa1
chile3 -> childOfchild3 -> childOfchild3.1 -> c1 -> a1 -> aa1
child3 -> childOfchild3 -> c1 -> a1 -> aa1
child3 -> c1 -> a1 -> aa1
child4

请帮助我正确地做到这一点。不需要使用递归来解决问题。感谢。

1 个答案:

答案 0 :(得分:-1)

public static void main(String[] args) throws IOException, SAXException {
        DocumentBuilder builder = JOOX.builder();
        Document xmlExampleDocument = builder.parse(XMLTEST.class.getResourceAsStream("/sample.xml"));
        List<String> doneList = $(xmlExampleDocument).xpath("//*[not(*)]").map(context -> $(context).xpath() + "='" + $(context).text() + "'");
        for (String x : doneList) {
            System.out.println(x);
        }
    }