将 xml 字符串作为返回 null 的文档传递 [英] Passing xml string as document returning null

查看:45
本文介绍了将 xml 字符串作为返回 null 的文档传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have seen many similar questions on stackoverflow and tried a lot but still no success. So posting my problem.

这是我的程序:

  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.我不确定我错过了什么.

Here document is returning null. I am not sure what I am missing.

推荐答案

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

The org.w3c.dom.Node#getTextContent method promises to return null when invoked on:

  • DOCUMENT_NODE
  • DOCUMENT_TYPE_NODE
  • NOTATION_NODE

查看文档这里.

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

If you want to verify your Document contains what's expected, or generally iterate over its nodes, you can iterate over getChildNodes.

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

Here's a little recursive method that'll print some debugging info for that XML string, in a self-contained example.

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]

这篇关于将 xml 字符串作为返回 null 的文档传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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