Java XML分析器问题

时间:2012-03-05 19:22:10

标签: java xml parsing

我正在尝试编写一个xml解析器,但在从文件中提取数据时似乎遇到了一些问题。我第一次调用get Tag Value时一直遇到java Null指针异常:

Root Element: integrity_bug_data
Getting Tage Value for: id
java.lang.NullPointerException

这是文件的样子:

<bug>
        <fields>
            <field name="ID">test</field>
            <field name="Summary"></field>
            <field name="Customer Importance">Unspecified</field>
            <field name="Development Order">Unspecified</field>
            <field name="Created Date">Mon Mar 05 03:41:46 EST 2012</field>
            <field name="Activity">Production</field>
            <field name="State">Triage</field>
            <field name="Date Closed"/>
            <field name="Date Found">Mon Mar 05 00:00:00 EST 2012</field>
            <field name="Issue Type">Software Report</field>
            <field name="Reason">Triage</field>
            <field name="Description">Subject:</field>
        </fields>
    </bug>

我写了(在阅读了一些教程之后):

 try{
        ArrayList<String> idData = new ArrayList<>(), sumryData = new ArrayList<>(), descpData = new ArrayList<>() ;

            //Load the XML File 
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document doc = docBuilder.parse(new File("./Data.xml"));

        System.out.println("Root Element: "+doc.getDocumentElement().getNodeName());

        NodeList bugList = doc.getElementsByTagName("bug");

        for (int i = 0; i < bugList.getLength(); i++){
            Node nNode = bugList.item(i);


            if(nNode.getNodeType() == Node.ELEMENT_NODE){
                Element eElement = (Element) nNode;

                idData.add(getTagValue("id",eElement));
                System.out.print("ID: "+getTagValue("id",eElement));


                String sumry = getTagValue("Summary",eElement);
                System.out.println("NodeList: "+sumry);

                String nstr = sumry.replace("\n", " ");
                String ustr = nstr.replaceAll(":", "");
                nstr = ustr.replace(",", " ");

                sumryData.add(nstr);
                System.out.print("Summary: "+nstr);


                String desc = getTagValue("Description",eElement);
                System.out.println("NodeList: "+desc);

                nstr = desc.replace("\n", " ");
                ustr = nstr.replaceAll(":", "");
                nstr = ustr.replace(",", " ");

                descpData.add(nstr);
                System.out.print("Description: "+nstr);
            }
        }

     public static String getTagValue(String tag, Element eElement){
        System.out.println("Getting Tage Value for: "+tag);
        NodeList n1List = eElement.getElementsByTagName(tag).item(0).getChildNodes();
        Node nValue = (Node) n1List.item(0);
        return nValue.getNodeValue();
    }    

这个问题可能与 <field name"..."></field>

,我应该在<id>, <summary> etc...阅读的地方?

提前感谢您的帮助...

3 个答案:

答案 0 :(得分:1)

如果<bug>不包含<id>元素,或者<id>标记没有子节点,则会发生这种情况。您在getTagValue()中不允许这两种可能性。

答案 1 :(得分:1)

由于您还没有写出您的问题,我无法帮助您使用您的代码。但我可以帮助解析这个XML。我建议你使用JAXB进行解析。这里你唯一需要的是创建类Bug

@XmlRootElement
public class Bug {
    private Collection<Field> fields;

    @XmlElementWrapper(name = "cars")
    @XmlElement(name = "car")    
    public Collection<Field> getFields() {
        return fields;
    }
    // other methods....
}


public class Field {
    private Sting name;
    private String text;

    @XmlAttribute
    public String getName() {
        return name;
    }
    @XmlValue
    public String getText() {
        return text;
    }

}

现在编写如下代码:

JAXBContext ctx = JAXBContext.newInstance("com.panpwr.api.model.deployment");
Unmarshaller unmarshaller = ctx.createUnmarshaller();
Data data = (Data)unmarshaller.unmarshal(in); // in is the input stream for XML
// now your instance of data is populated from XML

就是这样。你完成了。这将解析您的XML而不会出现任何问题。

答案 2 :(得分:1)

看起来您需要将getTagValue方法更改为以下内容:

public static String getTagValue(String tag, Element eElement) {
    System.out.println("Getting Tage Value for: " + tag);
    NodeList n1List = eElement.getElementsByTagName("field");
    for (int i = 0; i < n1List.getLength(); i++) {
        Node nNode = n1List.item(i);
        if (((Element) nNode).getAttribute("name").equalsIgnoreCase(tag)) {
            return nNode.getTextContent();
        }
    }
    return null;
}