使用DOM解析器解析XML文档,每个标记包含多个元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用DOM解析器解析XML文档,每个标记包含多个元素相关的知识,希望对你有一定的参考价值。

我需要解析具有相同标记名称的XML文档。我给你一个这段代码的样本,看看我想做什么..

<SystemData>
    <SystemName>xmlexample</SystemName>
    <Schools>
        <School>
            <SchoolName>SCHOOL1</SchoolName>
            <Classes>
                <Class>
                    <ClassName>ACLASS</ClassName>
                </Class>
            </Classes>
            <Classes>
                <Class>
                    <ClassName>BCLASS</ClassName>
                </Class>
            </Classes>
        </School>

        <School>
            <SchoolName>SCHOOL2</SchoolName>
            <Classes>
                <Class>
                    <ClassName>CCLASS</ClassName>
                </Class>
            </Classes>
        </School>
    </Schools>
</SystemData>

我想要的结果是:

SCHOOL1
ACLASS
BCLASS

SCHOOL2
CCLASS

我正在尝试for循环,但它让我为每个学校的所有课程..

我的代码到目前为止:

NodeList schoolist = doc.getElementsByTagName("School");
int num = schoolist.getLength();

for (int temp = 0; temp < num; temp++) {
    Node nNode = schoolist.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) schoolist.item(temp);
        Node schoolname = eElement.getFirstChild();

        System.out.println("schoolname: " + eElement.getElementsByTagName("ClassName").item(0).getTextContent());
    }

    NodeList Classlist = doc.getElementsByTagName("Method");
    int num1 = Classlist.getLength();

    for (int i = 0; i < num1; i++) {

        Node nNode1 = Classlist.item(i);

        if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement1 = (Element) Classlist.item(i);
            Node Classname = eElement1.getFirstChild();
            System.out.println("Classname: " + eElement1.getElementsByTagName("ClassName").item(0).getTextContent());

        }
    }
}
答案

所以最后我设法修复了代码..这就是它的工作原理..

        NodeList listOfSchool = doc.getElementsByTagName("School");


            for(int s=0; s<listOfSchool.getLength() ; s++){


                Node firstSchoolNode = listOfSchool.item(s);
                if(firstSchoolNode .getNodeType() == Node.ELEMENT_NODE){


                    Element SchoolElement = (Element)firstSchoolNode ;

                    //-------
                    NodeList SchoolNameList = SchoolElement.getElementsByTagName("SchoolName");
                    Element SchoolNameElement = (Element)SchoolNameList .item(0);

                    NodeList textFNList = SchoolNameElement.getChildNodes();
                    System.out.println("School Name : " + 
                           ((Node)textFNList.item(0)).getNodeValue().trim());

                        //----

                   NodeList listOfSchoolNames = SchoolElement.getElementsByTagName("Class");

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


                Node firstClassNode = listOfSchoolNames.item(i);
                 if(firstClassNode .getNodeType() == Node.ELEMENT_NODE){

                    Element classElement = (Element)firstClassNode;
                    //----

                        NodeList ClassNameList = classElement .getElementsByTagName("ClassName");


                        Element ClassNameElement = (Element)ClassNameList.item(0);

                         NodeList textCLSNMList = ClassNameElement.getChildNodes();
                           System.out.println("class Name : " + 
                           ((Node)textCLSNMList .item(0)).getNodeValue().trim());

                           }
}
另一答案

标签名称的一些明显问题:

System.out.println("schoolname: " + eElement.getElementsByTagName("ClassName").item(0).getTextContent());

应该

System.out.println("schoolname: " + eElement.getElementsByTagName("SchoolName").item(0).getTextContent());

NodeList Classlist = doc.getElementsByTagName("Method");

应该

NodeList Classlist = doc.getElementsByTagName("Class");

然后,你不想从文档中获取所有Class元素,但是所有Class文档都是当前学校的孩子。

另一答案

我建议在Java SE 5及更高版本中使用javax.xml.xpath API而不是getElementsByTagName,以使您的代码更易读,更易于维护。

import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xPath = xpf.newXPath();
        XPathExpression schoolNameExpression = xPath.compile("SchoolName");
        XPathExpression classNameExpression = xPath.compile("Classes/Class/ClassName");

        InputSource inputSource = new InputSource("src/forum14671896/input.xml");
        NodeList schoolNodes = (NodeList) xPath.evaluate("/SystemData/Schools/School", inputSource, XPathConstants.NODESET);
        for(int x=0; x<schoolNodes.getLength(); x++) {
            Node schoolElement = schoolNodes.item(x);

            // School Name
            System.out.print("School Name : ");
            System.out.println(schoolNameExpression.evaluate(schoolElement, XPathConstants.STRING));

            // Class Names
            NodeList classNames = (NodeList) classNameExpression.evaluate(schoolElement, XPathConstants.NODESET);
            for(int y=0; y<classNames.getLength(); y++) {
                System.out.print("Class Name : ");
                System.out.println(classNames.item(y).getTextContent());
            }
            System.out.println();
        }
    }

}

产量

School Name : SCHOOL1
Class Name : ACLASS
Class Name : BCLASS

School Name : SCHOOL2
Class Name : CCLASS

以上是关于使用DOM解析器解析XML文档,每个标记包含多个元素的主要内容,如果未能解决你的问题,请参考以下文章

xml_解析_解析方式和xml_常见的解析器

源生API解析XML文档与dom4j解析XML文档

Java:简单的解析XML文件之使用DOM解析

XML DOM 解析器

如何用Dom4j获取CDATA结点内容

Python XML 解析