Java从XML保存/读取数据 - 解析异常

时间:2013-09-29 13:27:34

标签: java xml

我的小聊天客户端软件使用XML文件存储聊天记录,我的处理读/写的课程是:

public class History {

    public String filePath;

    public History(String filePath) {
        this.filePath = filePath;
    }

    public String stripNonValidXMLCharacters(String in ) {
        StringBuffer out = new StringBuffer(); // Used to hold the output.
        char current; // Used to reference the current character.

        if ( in == null || ("".equals( in ))) return ""; // vacancy test.
        for (int i = 0; i < in .length(); i++) {
            current = in .charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should not happen.
            if ((current == 0x9) ||
                (current == 0xA) ||
                (current == 0xD) ||
                ((current >= 0x20) && (current <= 0xD7FF)) ||
                ((current >= 0xE000) && (current <= 0xFFFD)) ||
                ((current >= 0x10000) && (current <= 0x10FFFF)))
                out.append(current);
        }
        return out.toString();
    }

    public synchronized void addMessage(String from, String agentName, String msg, String time, String channel) {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            org.w3c.dom.Document doc = docBuilder.parse(filePath);

            Node data = doc.getFirstChild();

            org.w3c.dom.Element root = doc.createElement(channel);
            org.w3c.dom.Element message = doc.createElement("message");
            org.w3c.dom.Element _sender = doc.createElement("sender");
            _sender.setTextContent(stripNonValidXMLCharacters(from));
            org.w3c.dom.Element _content = doc.createElement("content");
            _content.setTextContent(stripNonValidXMLCharacters(msg));
            org.w3c.dom.Element _recipient = doc.createElement("recipient");
            _recipient.setTextContent(stripNonValidXMLCharacters(agentName));
            org.w3c.dom.Element _time = doc.createElement("time");
            _time.setTextContent(stripNonValidXMLCharacters(time));


            message.appendChild(_sender);
            message.appendChild(_content);
            message.appendChild(_recipient);
            message.appendChild(_time);
            root.appendChild(message);

            data.appendChild(root);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(filePath));
            transformer.transform(source, result);

        } catch (Exception ex) {
            System.out.println(ex.getStackTrace());
            // This is being trown randomly
        }
    }

    public synchronized void getHistory(String channel) {
        try {
            File fXmlFile = new File(filePath);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName(channel);

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    org.w3c.dom.Element eElement = (org.w3c.dom.Element) nNode;

                    if (getTagValue("sender", eElement).contains("Serv1")) {
                        printServerMsg(getTagValue("content", eElement).replace("%27", "'"), "", getTagValue("time", eElement));
                    } else {
                        printMsg(getTagValue("content", eElement).replace("%27", "'"), getTagValue("sender", eElement), getTagValue("time", eElement));
                    }

                }
            }
        } catch (Exception ex) {
            System.out.println("Filling Exception");

            // This is also being trown
        }
    }

    public String getTagValue(String sTag, org.w3c.dom.Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
    }
}

写作时我遇到的例外情况是: `INVALID_CHARACTER_ERR:指定了无效或非法的XML字符.`

阅读例外Filling exception 这里的例外是java.lang.NullPointerException

任何想法我怎么能保证这不会发生?问题是这基本上打破了整个客户端

1 个答案:

答案 0 :(得分:1)

而不是

System.out.println("Filling Exception");

总是使用

e.printStackTrace();

或者正确的日志框架,如log4j,并正确记录错误:

log.error("Filling Exception", e);

为什么这很重要?

因为通过这样做,您可以为我们提供完整的堆栈跟踪...

此外,为了转义字符串以用作XML内容,使用已经编写良好且经过验证的实用程序(如Apache commons StringEscapeUtils而不是重新发明轮子)是明智的

修改

来自OPs评论

  

哦,谢谢,我现在可以看到错误发生在getTagValue()

这里有NPE的多种可能性,但是杯线在这一行

NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

以下可以为null

eElement.getElementsByTagName(sTag).item(0)

如果文档中没有该名称的标记。

来自the documentation

  

返回集合中的索引项。如果index大于或等于列表中的节点数,则此将返回null。

所以我会改写这样的方法:

public String getTagValue(String sTag, org.w3c.dom.Element eElement) {
    Node n = eElement.getElementsByTagName(sTag).item(0);
    if(e !=null) {
        NodeList nlList = n.getChildNodes();
        Node nValue = (Node) nlList.item(0);
        if(nValue != null) {
            return nValue.getNodeValue();
        }
    }
    return ""; // or return null; if that is more applicable to the use case
}

另外要小心,现在这会返回一个空字符串,这可能会或可能不会很好:可以忽略所述节点不存在...