开发者

reading from XMl file in java

开发者 https://www.devze.com 2022-12-12 15:13 出处:网络
I have a simple XML file <requirements> <requirement> <name> SwitchON</name> <id>1</id>

I have a simple XML file

<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.

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)开发者_JS百科 {
            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); IdElement is null!!


Instead of all the looping and other xml ugliness, let me suggest a little helper method:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号