java - 将 xml 字符串作为文档传递返回 null

标签 java xml stringbuilder domparser

我在stackoverflow上看到过很多类似的问题,也尝试了很多,但仍然没有成功。所以发布我的问题。

这是我的程序:

  1. 获取 xml 格式的响应的 http 输出。
  2. 将响应存储在字符串中。
  3. 使用 xml dom 解析器解析字符串。
  4. 获取元素。

    <RESULTS>
<DEVICE name="qwewewew">
<PORT name="ilo">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>ilo</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
<PORT name="onboard-1">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>net</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
<PORT name="abncd">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE/>
<CONNECTS/>
</PORT>
<PORT name="abncd">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>fiber</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
<PORT name="power">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE/>
<CONNECTS/>
</PORT>
<PORT name="serial">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>serial</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
</DEVICE>
</RESULTS>

我的程序片段如下:

String baseUrl = "http://abcd.eng.xyz.com/wiremap/index.php?action=search&page=0&port_name=&dev_type=like&output=xml&dev_name=w2-fiqa-058";
String xmlRecords = get(baseUrl).asString();

DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();

factory2.setNamespaceAware(true);
DocumentBuilder builder2 = factory2.newDocumentBuilder();
Document document = builder2.parse(new ByteArrayInputStream(xmlRecords.getBytes()));
String test = document.getTextContent();
System.out.println("Value " +test);

System.out.println(document);

此处文档返回 null。我不确定我错过了什么。

最佳答案

org.w3c.dom.Node#getTextContent 方法 promise 在调用时返回 null:

  • DOCUMENT_NODE
  • DOCUMENT_TYPE_NODE
  • NOTATION_NODE

查看文档 here

如果您想验证您的 Document 包含预期的内容,或者通常迭代其节点,则可以迭代 getChildNodes

这是一个小递归方法,它将在一个独立的示例中打印该 XML 字符串的一些调试信息。

package test;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {

    public static void main(String[] args) throws Exception {
        String test = "<RESULTS><DEVICE name=\"qwewewew\"><PORT name=\"ilo\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>ilo</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"onboard-1\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>net</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"abncd\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE/><CONNECTS/></PORT><PORT name=\"abncd\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>fiber</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"power\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE/><CONNECTS/></PORT><PORT name=\"serial\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>serial</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT></DEVICE></RESULTS>";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(test.getBytes("UTF-8")));
        NodeList list = document.getChildNodes();
        recurse(list);
    }
    static void recurse(NodeList list) {
        if (list == null || list.getLength() == 0) {
            return;
        }
        else {
            for (int i = 0; i < list.getLength(); i++) {
                Node item = list.item(i);
                System.out.println(item);
                recurse(item.getChildNodes());
            }
        }
    }

}

输出

[RESULTS: null]
[DEVICE: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: ilo]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: net]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[CONNECTS: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: fiber]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[CONNECTS: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: serial]
[CONNECTS: null]
[#text: abncd]

关于java - 将 xml 字符串作为文档传递返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33098082/

相关文章:

java - 了解重写如何与 Java 优先级队列中的compareTo 配合使用?

java - deleteCharAt 或 setLength,哪种方法更适合从 StringBuilder/StringBuffer 中删除最后一个字符

java - 字符串加运算符和字符串生成器追加有什么优缺点?

c# - 字符串文字和字 rune 字可以连接吗?

java - 创建测试类

java - avro union的json编码

c# - LINQ 从 String[] 追加到 StringBuilder

c# - XML/XHTML 替换内容?

regex - XSLT - 通过分析文本节点添加新节点

c# - 将其转换为日期时间格式 C#