无法将 xml 字符串转换为 w3c 文档

发布于 2025-01-04 03:02:20 字数 2639 浏览 1 评论 0 原文

我想将包含 xml 的 java 字符串转换为 w3c dom 文档对象。

我首先到处搜索,并在 stackoverflow 上找到了一些很好的例子。 但遗憾的是我可以让他们工作!

显然我的代码不能 100% 工作。

看起来它解析了字符串,但节点中没有值。 这就是我到目前为止所得到的!

Document newDoc = null;

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(TestFiles.RSS_FEED_FILE_2));

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
builder = factory.newDocumentBuilder();
newDoc = builder.parse(is);

当我之后像这样执行 sysout 时:

System.out.println(newDoc.getDocumentElement().getElementsByTagName("channel").item(0)
.getNodeValue());

我在使用此 sysout 时得到了 null 作为输出:

System.out.println(newDoc.getDocumentElement().getElementsByTagName("channel").item(0));

我得到了输出: [channel: null]

所以我有一个对象,否则它会抛出一些空指针异常,但它不包含任何值里面 ?!

常量的内容是这样的:

public final static String RSS_FEED_FILE_2 =    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
                                            "<rss version=\"2.0\">\n" + 
                                            "<channel>\n" + 
                                            "<title>sunday</title>\n" + 
                                            "<link>http://www.google.nl</link>\n" + 
                                            "<pubDate>2012-02-05 20:58</pubDate>\n" + 
                                            "<lastBuildDate>2012-02-08 09:48</lastBuildDate>\n" + 
                                            "<description>blabla </description>\n" + 
                                            "<item>\n" + 
                                            "<title><![CDATA[title]]></title>\n" + 
                                            "<link><![CDATA[http://www.google.nl]]></link>\n" + 
                                            "<guid><![CDATA[2266610]]></guid>\n" + 
                                            "<source><![CDATA[sunday]]></source>\n" + 
                                            "<author><![CDATA[me]]></author>\n" + 
                                            "<description><![CDATA[blalbalavblabllllll!]]></description>\n" + 
                                            "</item>\n" + 
                                            "</channel>\n" + 
                                            "</rss>";

有人有解决方案或提示吗?

I want to convert an java string containing xml to a w3c dom document object.

I first searched all over the place and came up with some good examples here on stackoverflow.
But sadly I can get them working!

Apperently my code is not working 100%.

It seems like it parses the string but there are no values in the nodes.
This is what I got so far!

Document newDoc = null;

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(TestFiles.RSS_FEED_FILE_2));

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
builder = factory.newDocumentBuilder();
newDoc = builder.parse(is);

When I do a sysout afterwards like this:

System.out.println(newDoc.getDocumentElement().getElementsByTagName("channel").item(0)
.getNodeValue());

I got null as output while using this sysout:

System.out.println(newDoc.getDocumentElement().getElementsByTagName("channel").item(0));

I got as output: [channel: null]

So I have an object else it would throw some null pointer exceptions but it doesn't contain any values inside ?!

The content of the constant is this :

public final static String RSS_FEED_FILE_2 =    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
                                            "<rss version=\"2.0\">\n" + 
                                            "<channel>\n" + 
                                            "<title>sunday</title>\n" + 
                                            "<link>http://www.google.nl</link>\n" + 
                                            "<pubDate>2012-02-05 20:58</pubDate>\n" + 
                                            "<lastBuildDate>2012-02-08 09:48</lastBuildDate>\n" + 
                                            "<description>blabla </description>\n" + 
                                            "<item>\n" + 
                                            "<title><![CDATA[title]]></title>\n" + 
                                            "<link><![CDATA[http://www.google.nl]]></link>\n" + 
                                            "<guid><![CDATA[2266610]]></guid>\n" + 
                                            "<source><![CDATA[sunday]]></source>\n" + 
                                            "<author><![CDATA[me]]></author>\n" + 
                                            "<description><![CDATA[blalbalavblabllllll!]]></description>\n" + 
                                            "</item>\n" + 
                                            "</channel>\n" + 
                                            "</rss>";

Does anybody have a solution or a hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

度的依靠╰つ 2025-01-11 03:02:20

这是一个很常见的问题。 getNodeValue() 的行为取决于 节点。对于ElementgetNodeValue()始终返回null(请参阅 >Node 其他子类行为的 javadoc)。

如果您想调试 XML 文档,请考虑使用 getTextContent()

This is quite a common gotcha. The behaviour of getNodeValue() depends on the subclass of Node. In the case of an Element, getNodeValue() will always return null (see the table in the Node javadoc for behaviour of other subclasses).

Consider using getTextContent() if you want to debug the XML document.

匿名的好友 2025-01-11 03:02:20

当您尝试加载 RSS XML 字符串时,我建议您使用 http://www.thearchitect.co.uk/schemas/rss-2_0.xsd。这将帮助您加载 RSS 字符串,并为您提供更好的方法来编辑数据或将其转换到任何目的地(例如文件)。但这需要 JAXB 才能工作。希望这有帮助。

As you are trying to load an RSS XML string, I can suggest you to use RSS XSD from http://www.thearchitect.co.uk/schemas/rss-2_0.xsd. This will help you in loading the RSS string and giving you a better way to either edit data or transform it to any destinations like file. This will need JAXB to work although. Hope this helps.

草莓味的萝莉 2025-01-11 03:02:20

使用 jdom 处理 XML 会很痛苦,而且它通常是我的第一个停靠点。

如果可以选择使用 jdom,那么构建文档就很简单了。

SAXBuilder builder = new SAXBuiler();
Document doc = builder.build(new StringReader(YOUR_XML_STRING));

需要注意的是,这会创建一个 org.jdom.Document 对象,然后您需要将其适应 w3c 文档。同样,使用 org.jdom.output.DOMOutputter 类可以轻松实现这一点。

Using jdom takes a lot of pain of out processing XML, and it is usually my first port of call.

If using jdom is an option, then building the document is trivial.

SAXBuilder builder = new SAXBuiler();
Document doc = builder.build(new StringReader(YOUR_XML_STRING));

The thing to be careful of is that this creates an org.jdom.Document object, which you then need to adapt in to a w3c document. Again this is quite easily achieved with the org.jdom.output.DOMOutputter class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文