1

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

        }
    }
}
4

3 回答 3

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

                           }
}
于 2013-02-03T14:56:16.460 回答
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的子文档中获取所有文档。

于 2013-02-03T12:02:21.520 回答
0

我建议使用javax.xml.xpathJava SE 5 及更高版本中的 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
于 2013-02-04T11:30:10.387 回答