繁体   English   中英

获取第一个节点的节点值

[英]Get the Node value for the first Node

我有以下 XML:

<?xml version='1.0' ?>
<foo>A&gt;B</foo>

并且只想将开始标记的节点值获取为A&gt;B ,如果我们使用 getNodeValue ,它会将其转换为不需要的 A>B 。

因此我决定使用 Transformer

        Document doc = getParsedDoc(abovexml);
        TransformerFactory tranFact = TransformerFactory.newInstance();
        Transformer transfor = tranFact.newTransformer();
        transfor.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        Source src = new DOMSource(node);
        StringWriter buffer = new StringWriter();
        Result dest = new StreamResult(buffer);
        transfor.transform(src, dest);
        String result = buffer.toString();

但这给出了以下 output 作为<foo>A&gt;B</foo>结果的一部分

如果有人能澄清一下,如果有一种方法可以让我们在不从上述 output ( <foo>A&gt;B</foo> ) 中进行字符串操作的情况下获得A&gt;B将很有帮助

由于 getNodeValue() 会自动解码字符串。
您可以使用 Apache Commons Lang 中的 StringEscapeUtils 再次对其进行编码。

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
http://commons.apache.org/lang/

String nodeValue = StringEscapeUtils.escapeHtml(getNodeValue());

这会将其编码为您希望它采用的格式。它对性能不是很友好,因为您正在为每个节点值应用编码。

实际上 getNodeValue() 不是“转换”字符串。 当从文件中解析 XML 或通过转换生成时,得到的信息 model 是字符串A>B ,而不是A&gt;B 后者只是一种序列化形式。

另一种合法的序列化形式是A>B (因为在大多数情况下不需要转义右尖括号)。 但是,想要生产A&gt;B可能存在兼容性原因,特别是如果您的 output 打算成为 HTML (尽管您没有提到)。

如果您对 escaping 有充分的理由> ,那么我同意@kensen john 的回答。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM