如何将xml元素及其子节点转换为Java中的String? [英] How to convert xml Element and its child nodes into String in Java?

查看:429
本文介绍了如何将xml元素及其子节点转换为Java中的String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将XML节点及其子节点转换为带有节点名称的字符串。

Hi I want to convert XML node and its child into a string with its node names.

例如,我从XML文档中获取一个节点,看起来像这样:

For example I get a node from XML document which is look like this:

<Name>
  <Work>*86</Work>
  <Home>*86</Home>
  <Mobile>*80</Mobile> 
  <Work>0</Work>
</Name>

我想将整个节点转换成字符串。用nodenames,不仅文字。在这方面的任何帮助非常感谢。谢谢。

I want to convert whole node into string. With nodenames, not only text. Any help in this regards is greatly appreciated. Thanks.

推荐答案

可以使用JDom XMLOutputter ,条件是您的Element是一个org.jdom.Element:

you can use JDom XMLOutputter subject to the condition that your Element is an org.jdom.Element:

XMLOutputter outp = new XMLOutputter();
String s = outp.outputString(your_jdom_element);

这篇关于如何将xml元素及其子节点转换为Java中的String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆