简体   繁体   English

从Java中的XMl文件读取

[英]reading from XMl file in java

I have a simple XML file 我有一个简单的XML文件

<requirements>
    <requirement>
        <name> SwitchON</name>
        <id>1</id>
        <text>The Light shall turn on when the Switch is on.</text>
    </requirement>
    <requirement>
        <name>SwitchOFF</name>
        <id>2</id>
        <text>The Light shall turn off when the Switch is off.</text>
    </requirement>
    <requirement>
        <name>Lightbulb</name>
        <id>3</id>
        <text>The Light bulb shall be connected </text>
    </requirement>
    <requirement>
        <name>Power</name>
        <id>4</id>
        <text>The Light shall have the power supply</text>
    </requirement>
</requirements>

I am trying to show the information in this file in a table model. 我试图在表模型中显示此文件中的信息。 I have a method (readFromXMl) that reads the XML file and returns a table model. 我有一个方法(readFromXMl),它读取XML文件并返回表模型。

public static RequirementTable readFromXMl(String fileName) {
        RequirementTable T = new RequirementTable();
        Requirement R = new Requirement();

        try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(fileName));
            doc.getDocumentElement().normalize();
            NodeList listOfRequirements = doc.getElementsByTagName("requirement");

            int test = listOfRequirements.getLength();
            System.out.println("Total no of people : " + test);


            for (int i = 0; i < listOfRequirements.getLength(); i++) {
                   Node RequirementNode = listOfRequirements.item(i);
                if (RequirementNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element RequirementElement = (Element) RequirementNode;



                    NodeList IdList = RequirementElement.getElementsByTagName("id");
                    Element IdElement = (Element) IdList.item(0);
                    NodeList textIdList = IdElement.getChildNodes();
                    R.setId(Integer.parseInt(textIdList.item(0).getNodeValue()));

                    NodeList DescriptionList = RequirementElement.getElementsByTagName("text");
                    Element DescriptionElement = (Element) DescriptionList.item(0);
                    NodeList textDescriptionList = DescriptionElement.getChildNodes();
                    R.setText(textDescriptionList.item(0).toString());

                    NodeList NameList = RequirementElement.getElementsByTagName("name");
                    Element NameElement = (Element) NameList;
                    NodeList textNameList = NameElement.getChildNodes();



                    if (textNameList.item(0).toString().equals("SwitchON")) {
                        T.addRequirement((SwitchOnReq)R);
                    } else if (textNameList.item(0).toString().equals("SwitchOFF")) {
                        T.addRequirement((SwitchOFFReq)R);
                    } else if (textNameList.item(0).toString().equals("LightBulb")) {
                        T.addRequirement((BulbRequirement)R);
                    } else if (textNameList.item(0).toString().equals("Power")) {
                        T.addRequirement((PowerRequirement)R);
                    }
}
}
} catch (SAXParseException err) {
            System.out.println("** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId());
            System.out.println(" " + err.getMessage());

        } catch (SAXException e) {
            Exception x = e.getException();
            ((x == null) ? e : x).printStackTrace();

        } catch (Throwable t) {
            t.printStackTrace();
        }
        return T;

    }

However in this line I am getting an error which says the the pointer is null Element IdElement = (Element) IdList.item(0); 但是,在这一行中,我收到一个错误,指出指针为空Element IdElement = (Element) IdList.item(0); IdElement is null!! IdElement为空!

Instead of all the looping and other xml ugliness, let me suggest a little helper method: 让我建议一些辅助方法,而不是所有循环和其他xml丑陋的方法:

private static String getNodeValue(Node n, String path)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    return (String) xpath.evaluate(path, n, XPathConstants.STRING);
}

Use like this: 像这样使用:

for (int i = 0; i < listOfRequirements.getLength(); i++) {
Node RequirementNode = listOfRequirements.item(i);
System.out.println("name:" + getNodeValue(RequirementNode, "name"));
System.out.println("id:" + getNodeValue(RequirementNode, "id"));
System.out.println("text:" + getNodeValue(RequirementNode, "text"));

... ...

to get all the values and set your requirements. 获取所有值并设置您的要求。

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

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