简体   繁体   English

java - 如何使用DOM解析器在java中解析具有相同标签名称的嵌套XML?

[英]How to parse nested XML with identical tag names in java using DOM parser?

I have to put the values of "s" and "r" tags in map of arraylist.我必须将“s”和“r”标签的值放在arraylist 的映射中。 So, I want to get their values.所以,我想得到他们的价值观。 I am confused about how to get the values of nested identical tags.我对如何获取嵌套相同标签的值感到困惑。 Can someone help me to do that?有人可以帮我这样做吗? Thanks in advance!提前致谢!

Demo.xml file演示.xml 文件

<?xml version="1.0"?>
<Record>
    <app_name>app1</app_name>
    <faults>
        <Record>
            <s>1 </s>
            <r>Fixed </r>
        </Record>
        <Record>
            <s>2 </s>
            <r>New </r>
        </Record>
        <Record>
            <s>1 </s>
            <r>Fixed </r>
        </Record>
    </faults>
</Record>

<Record>
    <app_name>app2</app_name>
    <faults>
        <Record>
            <s>0 </s>
            <r>New </r>
        </Record>
        <Record>
            <s>3 </s>
            <r>New </r>
        </Record>
        <Record>
            <s>1 </s>
            <r>Fixed </r>
        </Record>
    </faults>
</Record>

The code done so far:到目前为止完成的代码:

File xmlFile=new File("demo.xml");
                byte[] b= Files.readAllBytes(xmlFile.toPath());
                String xml=new String(b);
                DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newDefaultInstance();
                DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
                Document doc=dBuilder.parse(new InputSource(new StringReader(xml)));
                NodeList Records=doc.getElementsByTagName("Records");
                NodeList RecordOuter,RecordInnerList,RecordInner,faults,faultChildren;
                Node faultNode;
                for(int i=0;i<Records.getLength();i++)
                {
                    RecordOuter=((Element)Records.item(i)).getElementsByTagName("Record");
                    faults=((Element)RecordOuter.item(0)).getElementsByTagName("faults");
                    faultChildren=faults.item(0).getChildNodes();
                    for(int j=0;j<faultChildren.getLength();j++)
                    {
                        faultNode=faultChildren.item(j);
                        RecordInnerList=faultNode.getChildNodes();
                        for(int k=0;k<RecordInnerList.getLength();k++)
                        {
                            RecordInner=(NodeList) RecordInnerList.item(k);
                            NodeList RecordInnerChildren=RecordInner.item(1).getChildNodes();
                        }
                    }
                }
            }

I am not getting expected result from this code!我没有从这段代码中得到预期的结果!

The method getElementsByTagName() searches all descendants, not just immediate children, so it's the wrong choice if you want to process the document one level at a time using nested loops. getElementsByTagName()方法搜索所有后代,而不仅仅是直接子代,因此如果您想使用嵌套循环一次处理一层文档,那么这是错误的选择。

Does it have to be DOM?它必须是DOM吗? Other more recent Java libraries for XML (such as JDOM2 or XOM) are so much nicer.其他较新的用于 XML 的 Java 库(例如 JDOM2 或 XOM)要好得多。 Personally, I would always use higher-level languages such as XSLT/XQuery/XPath.就个人而言,我总是会使用更高级的语言,例如 XSLT/XQuery/XPath。 But without knowing what your project is trying to achieve, or what the constraints are, I can't advise on that.但在不知道您的项目试图实现什么或限制是什么的情况下,我无法就此提出建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM