java中如何使用Dom4j获取XML的节点内容

发布于 2024-12-18 13:35:54 字数 519 浏览 1 评论 0原文

我有一个 XML 文件,如下所示:

<description>
  <text>blahblah</text>
  <code>code</code>
  <text>blah</text>
</description>

我已导航到节点 description,并且我想读取完整内容,包括 等。

我使用了 getText(),但它返回了空字符串。
我使用了 getStringValue(),但它过滤了所有
我使用了 asXML(),结果很接近,但结果包含我不想要的

有没有一种方法可以获取元素的 XML 内容?

I have a XML file like:

<description>
  <text>blahblah</text>
  <code>code</code>
  <text>blah</text>
</description>

I've navigated to the node description, and I want to read the full content including the <text> and so on.

I've used the getText(), but it returned empty string.
I've used the getStringValue(), but it filtered all <text>.
I've used the asXML(), the result is close, but the result contains <description> which I don't want.

Is there a method to get the XML content of a element?

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

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

发布评论

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

评论(4

慵挽 2024-12-25 13:35:54

像这样的事情:

public static void main(String[] args) throws DocumentException {
  String xml = "<description><text>blahblah</text><code>code</code><text>blah</text></description>";
  SAXReader reader = new SAXReader();
  Document doc = reader.read(new StringReader(xml));
  Element description = doc.getRootElement();
  String content = getContent(description);
  System.out.println(content);
}

private static String getContent(Element element) {
  StringBuilder builder = new StringBuilder();
  for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
    Element e = i.next();
    builder.append(e.asXML());
  }
  return builder.toString();
}

请注意,如果元素本身具有文本内容,则不会返回文本内容,只会返回子节点。

Something like this:

public static void main(String[] args) throws DocumentException {
  String xml = "<description><text>blahblah</text><code>code</code><text>blah</text></description>";
  SAXReader reader = new SAXReader();
  Document doc = reader.read(new StringReader(xml));
  Element description = doc.getRootElement();
  String content = getContent(description);
  System.out.println(content);
}

private static String getContent(Element element) {
  StringBuilder builder = new StringBuilder();
  for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
    Element e = i.next();
    builder.append(e.asXML());
  }
  return builder.toString();
}

Note that if the element has text content itself, this won't return the text content, only the child nodes.

从来不烧饼 2024-12-25 13:35:54

假设 documentorg.dom4j.Document 的实例,那么

String xPath = "description";
List<Node> nodes = document.selectNodes( xPath );
for (Node node : nodes) {
 node.asXML()
}

Assume that document is and instance of org.dom4j.Document, then

String xPath = "description";
List<Node> nodes = document.selectNodes( xPath );
for (Node node : nodes) {
 node.asXML()
}
等待我真够勒 2024-12-25 13:35:54

只是想添加到 qwerky 接受的答案中:

还能够解析纯文本元素的内容(即它不包含嵌套的 xml):

public static String getContent(Element element) {
    if (element.isTextOnly())
        return element.getText();
    StringBuilder sb = new StringBuilder();
    Element currElement = null;
    for (Iterator<Element> iterator = element.elementIterator() ; iterator.hasNext() ; /* Continue till done */) {
        currElement = iterator.next();
        sb.append(currElement.asXML());
    }
    return sb.toString();
}

所以我在方法的开头添加了以下内容:

if (element.isTextOnly())
    return element.getText();

Just want to add to the accepted answer by qwerky:

To ALSO be able to parse the contents of text only elements (ie it doesn't contain nested xml):

public static String getContent(Element element) {
    if (element.isTextOnly())
        return element.getText();
    StringBuilder sb = new StringBuilder();
    Element currElement = null;
    for (Iterator<Element> iterator = element.elementIterator() ; iterator.hasNext() ; /* Continue till done */) {
        currElement = iterator.next();
        sb.append(currElement.asXML());
    }
    return sb.toString();
}

So I added the following at the start of the method:

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