Java 文档生成器返回 Xml 字符串的空文档
Java Document builder returning null Document for Xml String
I have tried all possible answers 在 Whosebug 上,但我无法解决这个问题。
我有以下 xml 作为字符串:
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>";
参数可以换成任意字符串(我试过没成功),代码如下returns null document:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));
我尝试用以下内容替换文档:
Document document = builder.parse(new InputSource(new StringReader(xmlStr)));//This used to work 2 weeks ago.
document 一直返回空文档,我不知道我做错了什么。几周前使用的相同代码。
我正在使用 Java 1.8 !!
感谢您的帮助。
更新:
这是完整的代码,它采用上述 xmlStr 并尝试访问员工的年龄。
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。即它根本不进入循环。
它似乎工作正常。在 builder.parse
之后添加的以下代码表明正在解析 XML:
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
我有以下 xml 作为字符串:
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>";
参数可以换成任意字符串(我试过没成功),代码如下returns null document:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));
我尝试用以下内容替换文档:
Document document = builder.parse(new InputSource(new StringReader(xmlStr)));//This used to work 2 weeks ago.
document 一直返回空文档,我不知道我做错了什么。几周前使用的相同代码。
我正在使用 Java 1.8 !!
感谢您的帮助。
更新:
这是完整的代码,它采用上述 xmlStr 并尝试访问员工的年龄。
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。即它根本不进入循环。
它似乎工作正常。在 builder.parse
之后添加的以下代码表明正在解析 XML:
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