从java中的XMl文件中读取

时间:2009-11-24 15:44:27

标签: java xml

我有一个简单的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>

我试图在表格模型中显示此文件中的信息。 我有一个方法(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;

    }

但是在这一行中,我收到一个错误,指出指针为空 Element IdElement = (Element) IdList.item(0); IdElement为空!!

1 个答案:

答案 0 :(得分:1)

而不是所有的循环和其他xml丑陋,让我建议一个小帮手方法:

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

像这样使用:

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

...

获取所有值并设置您的要求。