如何使用 Dom4j 测试元素的 CDATA 值?

发布于 2024-12-14 17:28:54 字数 121 浏览 3 评论 0原文

有人知道如何找出元素是否包含 吗?我搜索了 dom4j API 和 Jaxen,但找不到如何执行此操作...如果我检索文本,则 cdata 包装器会被修剪。

Does anybody have an idea how to find out whether an element contains <![CDATA[ text ]]> or not ? I search through the dom4j API and Jaxen and I can't find how to do that... If I retrieve the text, it the cdata wrapper is trimmed.

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

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

发布评论

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

评论(3

临风闻羌笛 2024-12-21 17:29:12

http://dom4j.sourceforge .net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#getNodeType%28%29

这行得通吗?

public short getNodeType()

根据节点类型返回代码。这使得多态处理节点变得更加容易,因为可以使用 switch 语句而不是多个 if (instanceof) 语句。

返回:节点类型(例如 ELEMENT_NODE 或 ATTRIBUTE_NODE)的 W3C DOM 兼容代码

http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#getNodeType%28%29

will this work?

public short getNodeType()

Returns the code according to the type of node. This makes processing nodes polymorphically much easier as the switch statement can be used instead of multiple if (instanceof) statements.

Returns: a W3C DOM complient code for the node type such as ELEMENT_NODE or ATTRIBUTE_NODE

慕巷 2024-12-21 17:29:08

从技术上讲,你仍然可以这样做。


public boolean isCDATA(org.dom4j.Node node) {
  for (org.dom4j.Node n : node.content()) {
    if (org.w3c.dom.Node.CDATA_SECTION_NODE == n.getNodeType()) {
      return true;
    }
  }
  return false;
}

Technically you can still do this.


public boolean isCDATA(org.dom4j.Node node) {
  for (org.dom4j.Node n : node.content()) {
    if (org.w3c.dom.Node.CDATA_SECTION_NODE == n.getNodeType()) {
      return true;
    }
  }
  return false;
}
清风不识月 2024-12-21 17:29:02

该方法:

Node.asXML()

返回整个元素,其值未经任何修改。

因此,如果您有:

<nodes>
  <node><![CDATA[value]]></node>
</nodes>

调用文本方法将返回“value”,但调用“asXML()”将返回:

<node><![CDATA[value]]></node>

从那里,我想您可以对 CDATA 标记进行字符串搜索。

The method:

Node.asXML()

returns the entire element with its value unmodified by anything.

So if you have:

<nodes>
  <node><![CDATA[value]]></node>
</nodes>

Calling the text methods will return "value", but calling "asXML()" will return:

<node><![CDATA[value]]></node>

From there, I guess you can do a String search for the CDATA tag.

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