2015-11-01 77 views
0

我有这样的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打印带有值的节点路径。这里是我得输出的样本:

这是最后的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 

Plese帮助我使它正确无误。不需要使用递归来解决问题。谢谢。

+0

你能整理一下孩子的智利混乱吗? –

+2

此外,你能否解释为什么兄弟关系(child1和child2)似乎与父母关系(child3和childof3)的表现方式相同,这里的逻辑是什么?为什么有些标签只是在输出中跳过(如“step”)? –

+0

好吧,我正在编辑 – chatchai

回答

-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); 
     } 
    } 
+0

1.您的XML格式不正确。修理它。 2.考虑使用JOOX API:编译'org.jooq:joox:1.2.0' 3.共享示例代码。 –

+0

谢谢你的回答,但我从不使用JOOX API。阿米特Parashar plese帮助我一次。现在我使用DOM来解决问题 – chatchai

+0

在这种情况下,请遵循Blaise Doughan的解决方案,地址为http://stackoverflow.com/questions/4746299/generate-get-xpath-from-xml-node-java。这很好。 –