Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在xml中获取路径_Java_Xml - Fatal编程技术网

Java 如何在xml中获取路径

Java 如何在xml中获取路径,java,xml,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">

我有这样的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代码,但无法正常工作

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;
    }
请帮我把它弄好。不需要使用递归来解决问题。谢谢。

publicstaticvoidmain(String[]args)抛出IOException、SAXException{
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);
        }
    }
DocumentBuilder=JOOX.builder(); 文档xmlExampleDocument=builder.parse(XMLTEST.class.getResourceAsStream(“/sample.xml”); List doneList=$(xmlExampleDocument).xpath(“/*[非(*)]”).map(上下文->$(上下文).xpath()+“=”+$(上下文).text()+“”); for(字符串x:doneList){ 系统输出println(x); } }
你能整理一下孩子们的困惑吗?另外,你能解释一下为什么兄弟姐妹关系(
child1
child2
)看起来像是父母关系(
child3
childof3
),这里的逻辑是什么?为什么在输出中跳过了一些标记(例如
步骤
)?child1和child2有相同的父项是root,而一些标记消失在路径中,我不想显示。(child3和childof3)我的意思是childof3的父节点是child3,如果您不理解或混淆,我很抱歉。@BoristheSpider我编辑了xml1。您的XML格式不正确。修好它。2.考虑使用Joox API:编译“Org.Jooq:Joox:1.2.0”3。示例代码共享。感谢您的回答,但我从未使用过JooxAPI。Amit Parashar请再帮我一次。现在我使用DOM来解决问题在这种情况下,请按照Blaise Doughan的解决方案。这非常有效。为什么要使用IOUtils将InputStream转换为字符串,然后转换为字节(使用默认字符集,这通常是不安全的),然后再转换为InputStream?getResourceAsStream已返回完全可用且可解析的InputStream。@VGR。实际上,我刚刚从JOOX测试类中提取了示例代码。你说得对,这不是必需的。谢谢现在修好了。
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
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);
        }
    }