Java 文档生成器为 Xml 字符串返回空文档 [英] Java Document builder returning null Document for Xml String

查看:61
本文介绍了Java 文档生成器为 Xml 字符串返回空文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有可能的答案在 Stackoverflow 上,但我无法解决这个问题.

I have tried all possible answers on Stackoverflow but I can't get this resolved.

我有以下 xml 作为字符串:

I have the following xml as a String:

private static final String xmlStr = "<parameters>\n" +
            "  <parameter>\n" +
            "    <name>emp</name>\n" +
            "    <keyvalue>John Smith</keyvalue>\n" +
            "  </parameter>\n" +
            "  <parameter>\n" +
            "    <name>age</name>\n" +
            "    <keyvalue>22</keyvalue>\n" +
            "  </parameter>\n" +
            "  <parameter>\n" +
            "    <name>Birth Date</name>\n" +
            "    <keyvalue>02/05/1978</keyvalue>\n" +
            "  </parameter>\n" +
            "  <parameter>\n" +
            "    <name>password</name>\n" +
            "    <keyvalue>ye63633</keyvalue>\n" +
            "  </parameter>\n" +
            "</parameters>";

参数可以用任何字符串替换(我试过没有成功),下面的代码返回空文档:

The parameters can be replaced with any string (which I tried without sucess), and the following code returns null document:

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));

我尝试用以下内容替换文档:

I have tried replacing document with:

Document document = builder.parse(new InputSource(new StringReader(xmlStr)));//This used to work 2 weeks ago.

document 一直返回空文档,我不知道我做错了什么.几周前使用的相同代码.

document is returning null document all the time and I can't figure out what I am doing wrong. The same code used to work few weeks ago.

我使用的是 Java 1.8 !!

I am using Java 1.8 !!

我很感激你的帮助.

更新:

这是采用上述 xmlStr 并尝试访问员工年龄的完整代码.

Here is the full code that takes the above xmlStr and try to access age of the employee.

private String getElementValue(String tagName, String xmlString) {

        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new InputSource(new StringReader(xmlString)));
            Element rootElement = document.getDocumentElement();

            NodeList list = rootElement.getElementsByTagName(tagName);

            if (list != null && list.getLength() > 0) {
                NodeList subList = list.item(0).getChildNodes();

                if (subList != null && subList.getLength() > 0) {
                    return subList.item(0).getNodeValue();
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

根据评论,该文档根本不为空,但访问所需的 xml 标记存在问题.所以代码 NodeList list = rootElement.getElementsByTagName("age");大小为 0.即它根本不进入循环.

Based on the comments, the document is not null at all but there is an issue accessing the desired xml tag. So the code NodeList list = rootElement.getElementsByTagName("age"); has size 0. i.e its not entering the loop at all.

推荐答案

它似乎工作正常.在 builder.parse 之后添加的以下代码表明正在解析 XML:

It seems to be working correctly. The following code, added after builder.parse, shows that the XML is being parsed:

System.out.println("Root element: " + document.getDocumentElement().getNodeName());
NodeList nodeList = document.getElementsByTagName("parameter");
for (int temp = 0; temp < nodeList.getLength(); temp++) {
    org.w3c.dom.Node node = nodeList.item(temp);
    System.out.println("\nCurrent element: " + node.getNodeName());
    if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
        Element element = (Element) node;
        System.out.println("Name: " + element.getElementsByTagName("name").item(0).getTextContent());
        System.out.println("Key Value: " + element.getElementsByTagName("keyvalue").item(0).getTextContent());
    }
}

输出:

Root element: parameters

Current element: parameter
Name: emp
Key Value: John Smith

Current element: parameter
Name: age
Key Value: 22

Current element: parameter
Name: Birth Date
Key Value: 02/05/1978

Current element: parameter
Name: password
Key Value: ye63633

这篇关于Java 文档生成器为 Xml 字符串返回空文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆