Android:解析 XML DOM 解析器。将子节点转换为字符串
又问一个问题。这次我正在解析从服务器收到的 XML 消息。 有人自以为聪明,决定将 HTML 页面放入 XML 消息中。现在我遇到了一些问题,因为我想从该 XML 消息中将该 HTML 页面提取为字符串。
好的,这是我正在解析的 XML 消息:
您会看到 Param1 中指定了一个 HTML 页面。我尝试通过以下方式提取消息:
public String getParam1(Document d) { if (d.getDocumentElement().getTagName().equals("AmigoRequest")) { NodeList results = d.getElementsByTagName("Param1"); // Messagetype depends on what message we are reading. if (results.getLength() > 0 && results != null) { return results.item(0).getFirstChild().getNodeValue(); } } return ""; }
其中 d 是文档形式的 XML 消息。 它总是返回一个 null 值,因为 getNodeValue() 返回 null。 当我尝试 results.item(0).getFirstChild().hasChildNodes() 时,它将返回 true,因为他看到消息中有一个标签。
如何从字符串中的 Param0 中提取 html 消息 testTesthtml
?
我正在使用 Android sdk 1.5(几乎是 java)和 DOM 解析器。
感谢您的时间和回复。
安泰克
Again a question. This time I'm parsing XML messages I receive from a server.
Someone thought to be smart and decided to place HTML pages in a XML message. Now I'm kind of facing problems because I want to extract that HTML page as a string from this XML message.
Ok this is the XML message I'm parsing:
<AmigoRequest>
<From></From>
<To></To>
<MessageType>showMessage</MessageType>
<Param0>general message</Param0>
<Param1><html><head>test</head><body>Testhtml</body></html></Param1>
</AmigoRequest>
You see that in Param1 a HTML page is specified. I've tried to extract the message the following way:
public String getParam1(Document d) { if (d.getDocumentElement().getTagName().equals("AmigoRequest")) { NodeList results = d.getElementsByTagName("Param1"); // Messagetype depends on what message we are reading. if (results.getLength() > 0 && results != null) { return results.item(0).getFirstChild().getNodeValue(); } } return ""; }
Where d is the XML message in document form.
It always returns me a null value, because getNodeValue() returns null.
When i try results.item(0).getFirstChild().hasChildNodes() it will return true because he sees there is a tag in the message.
How can i extract the html message <html><head>test</head><body>Testhtml</body></html>
from Param0 in a string?
I'm using Android sdk 1.5 (well almost java) and a DOM Parser.
Thanks for your time and replies.
Antek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以获取 param1 的内容,如下所示:
您所要做的就是实现一个函数:
该函数将从字符串中删除所有出现的 HTML 标记。
为此,您可以查看这篇文章:从字符串中删除 HTML 标签
You could take the content of param1, like this:
All you have to do is to implement a function:
that will remove all HTML tag occurrences from a string.
For that you can take a look at this post: Remove HTML tags from a String
经过大量检查并挠头数千次后,我想出了一个简单的修改,需要将 API 级别更改为 8
after checking a lot and scratching my head thousands of times I came up with simple alteration that it needs to change your API level to 8
编辑:我刚刚看到您上面关于 Android 不支持
getTextContent()
的评论。我将保留这个答案,以防它对使用不同平台的人有用。如果您的 DOM API 支持,您可以调用
getTextContent()
,如下所示:但是,
getTextContent()
是 DOM Level 3 API 调用;并非所有解析器都保证支持它。 Xerces-J 确实。顺便说一下,在您原来的示例中,您对
null
的检查位于错误的位置;它应该是:否则,如果
results
确实返回为null
,您将得到 NPE。EDIT: I just saw your comment above about
getTextContent()
not being supported on Android. I'm going to leave this answer up in case it's useful to someone who's on a different platform.If your DOM API supports it, you can call
getTextContent()
, as follows:However,
getTextContent()
is a DOM Level 3 API call; not all parsers are guaranteed to support it. Xerces-J does.By the way, in your original example, your check for
null
is in the wrong place; it should be:Otherwise, you'd get a NPE if
results
really does come back asnull
.由于您无法使用
getTextContent()
,因此另一种选择是编写它 - 这并不难。事实上,如果您编写此内容仅供自己使用 - 或者您的雇主对开源没有过于严格的规则 - 您可以查看 Apache 的实现 作为起点;第 610-646 行似乎包含了您需要的大部分内容。 (请尊重 Apache 的版权和许可。)否则,该方法的一些粗略伪代码将是:
Since
getTextContent()
isn't available to you, another option would be to write it -- it isn't hard. In fact, if you're writing this solely for your own use -- or your employer doesn't have overly strict rules about open source -- you could look at Apache's implementation as a starting point; lines 610-646 seem to contain most of what you need. (Please be respectful of Apache's copyright and license.)Otherwise, some rough pseudocode for the method would be:
好吧,我的代码就快到了...
就像我的代码注释中所解释的那样。我所缺少的就是从文档中创建一个字符串。你不能在 Android 中使用 Transform 类... doc2.toString() 将为你提供对象的序列化..
但我的下一步是编写我自己的解析器,如果这不起作用;)
不是最好的代码,而是一个临时解决方案。
其中 String b 是 XML 文档字符串。
Well i was almost there with the code...
Like explained in the comment of my code. All I am missing is to make a String out of a Document. You can't use the Transform class in Android... doc2.toString() will give you a serialization of the object..
But my next step is write my own parser if this doesnt work out ;)
Not the best code but a temponary solution.
Where String b is the XML document string.