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

时间:2013-02-03 11:19:48

标签: java xml parsing dom xml-parsing

我需要解析具有相同标记名称的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());

        }
    }
}

3 个答案:

答案 0 :(得分:1)

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

        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());

                           }
}

答案 1 :(得分:0)

标签名称存在一些明显问题:

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个文档。

答案 2 :(得分:0)

我建议使用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