如何将xml的String字符串转化标准格式的String字符

 我来答
信玄居士72a5251
2015-09-27 · TA获得超过4406个赞
知道大有可为答主
回答量:3257
采纳率:100%
帮助的人:1803万
展开全部
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class ToolUtrils { /** * 字符串转化为XML串 * * @param str * @return * @throws Exception */ public static String strChangeToXML(String str) { SAXReader saxReader = new SAXReader(); Document document = null; try { document = saxReader.read(new ByteArrayInputStream(str.getBytes())); } catch (DocumentException e) { e.printStackTrace(); } StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UFT-8"); XMLWriter xmlwriter = new XMLWriter(writer, format); try { xmlwriter.write(document); } catch (IOException e) { e.printStackTrace(); } return writer.toString(); } /** * 测试程序文件 * * @param args */ public static void main(String[] args) { String str = "<?xml version='1.0' encoding='UTF-8'?><GESInfo><City id='苏州' name='苏州' code='1001'><Area id='市区' name='市区' code='10011001' LocationSetup='0'><Place id='观前' name='观前' code='100110011001' LocationSetup='1'><Monitor id='摄像头1' name='摄像头1' code='11000000000000000011200034800000' gesid='abcde1' channelid='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /> <Monitor id='摄像头2' name='摄像头2' code='11000000000000000011200034800000' gesid='abcde2' channelid='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /></Place><Place id='石路' name='石路' code='100110011002' LocationSetup='2'><Monitor id='摄像头3' name='摄像头3' code='11000000000000000011200034800000' gesid='abcde3' channelid='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /> <Monitor id='摄像头4' name='摄像头4' code='11000000000000000011200034800000' gesid='abcde4' channelid='
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式