如何将xml的String字符串转化标准格式的String字符串
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='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /> </Place></Area><Area id='园区' name='园区' code='10011002' LocationSetup='0'><Place id='科技园' name='科技园' code='100110011003' LocationSetup='1'><Monitor id='摄像头5' name='摄像头5' code='11000000000000000011200034800000' gesid='abcde5' channelid='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /> <Monitor id='摄像头6' name='摄像头6' code='11000000000000000011200034800000' gesid='abcde6' channelid='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /> </Place><Place id='金鸡湖' name='金鸡湖' code='100110011004' LocationSetup='2'><Monitor id='摄像头7' name='摄像头7' code='11000000000000000011200034800000' gesid='abcde7' channelid='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /> <Monitor id='摄像头8' name='摄像头8' code='11000000000000000011200034800000' gesid='abcde8' channelid='1' VideoScan='1' Capture='1' ManualREC='1' RecPlay ='1' MonControl='1' /> </Place></Area></City></GESInfo>";
System.out.println(strChangeToXML(str));
}
}
<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="1" VideoScan="1" Capture="1" ManualREC="1" RecPlay="1" MonControl="1"/>
</Place>
</Area>
<Area id="园区" name="园区" code="10011002" LocationSetup="0">
<Place id="科技园" name="科技园" code="100110011003" LocationSetup="1">
<Monitor id="摄像头5" name="摄像头5" code="11000000000000000011200034800000" gesid="abcde5" channelid="1" VideoScan="1" Capture="1" ManualREC="1" RecPlay="1" MonControl="1"/>
<Monitor id="摄像头6" name="摄像头6" code="11000000000000000011200034800000" gesid="abcde6" channelid="1" VideoScan="1" Capture="1" ManualREC="1" RecPlay="1" MonControl="1"/>
</Place>
<Place id="金鸡湖" name="金鸡湖" code="100110011004" LocationSetup="2">
<Monitor id="摄像头7" name="摄像头7" code="11000000000000000011200034800000" gesid="abcde7" channelid="1" VideoScan="1" Capture="1" ManualREC="1" RecPlay="1" MonControl="1"/>
<Monitor id="摄像头8" name="摄像头8" code="11000000000000000011200034800000" gesid="abcde8" channelid="1" VideoScan="1" Capture="1" ManualREC="1" RecPlay="1" MonControl="1"/>
</Place>
</Area>
</City>
</GESInfo>
转载于:https://blog.51cto.com/gaojie/197704
如何将xml的String字符串转化标准格式的String字符串相关推荐
- python把字符串转化为字典_python 将字符串转换为字典
在一般的工程处理中,需要将获取的字符串数据转换为字典,这样处理起来会非常方便. 我获取的是json数据: content = {"corpus_no":"64702772 ...
- python字符串转化列表_Python列表到字符串的转换
python字符串转化列表 Sometimes we want to convert the list to a string so that we can print it or log it fo ...
- java 将字符串转化成输入流_JAVA将字符串变为输入流
JAVA将字符串变为输入流 关于字符串转化为输入流,我找到2种方法: 用StringReader将字符串转化为Reader 用ByteArrayInputStream将字符串转化为InputStrea ...
- java字符串转化为数组_Go 语言字符串和数组转化 | 臭大佬
代码 package main import ( "fmt" "strconv" ) func main() { str := "17" i ...
- python把字符串转化为字典_python 将字符串转换成字典dict的各种方式总结
1)利用eval可以将字典格式的字符串与字典户转 >>>mstr = '{"name":"yct","age":10}' ...
- c 语言 string类型转换,用标准c++实现string与各种类型之间的转换
要实现这个目标,非stringstream类莫属. 这个类在头文件中定义, < sstream>库定义了三种类:istringstream.ostringstream和stringstre ...
- php 字符串转化字符集,php convert_cyr_string 将字符串由一种 Cyrillic 字符集转换成另一种...
convert_cyr_string - 将字符由一种 Cyrillic 字符转换成另一种 convert_cyr_string 函数基本语法介绍: convert_cyr_string(str,fr ...
- 将字符串转化为指定长度的字符串
http://cn2.php.net/manual/zh/function.str-pad.php
- php将字符串转换为json格式,js中将字符串转换为json格式的三种方法
json在js的开发过程中经常会用到,像在使用ajax开发的项目过程中,经常需要将json格式的字符串返回到前端,前端解析成json对象. 下面为大家介绍下将字符串转换为json对象的三种常用的方法: ...
最新文章
- php的框架目录,Laravel 框架目录结构
- 创建WCF第一个应用程序
- Python基础(14)_python模块之configparser模块、suprocess
- 基本操作:win10系统磁盘分区
- 基于SegNet和UNet的遥感图像分割代码解读
- 三种常见单片机时钟电路方案,对比其优缺点
- OpenCV-Python bindings是如何生成的(1)
- 怎样在数据绑定到DATAGRID之前,先判断其中一个字段是否为空?
- AIR中文帮助 第十章. 窗体(Windows)和菜单
- 随想录(在x86 linux上仿真多核cpu运行)
- Python基础知识汇总
- php 开发商城 注意,开发PHP商城要注意的一些常见安全问题
- Arduboy 游戏机制作参考教程
- .NET Interop.SHDocVw和MSHTML引用如何操作
- pcb怎么画边框_PCB设计--PCB画图技巧
- 苹果电脑u盘装win7系统教程
- js实现上传图片之后回显
- 论文引用图片时的版权声明(Reproduced with permission, courtesy of [copyright owner])
- CSS3中的元素过渡属性transition
- 苹果宣布换芯 背后究竟硬气何在?