如何将xml的String字符串转化标准格式的String字符串
2009-09-01 17:12
405 查看
最近,媳妇要做一个String串的格式化处理,一开始采用dom4j转化为Document对象的形式,结果不起作用,后来还是采用dom4j的方式,最终输出了一个规则的XML的String字符串,下面看看如何实现的。
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='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));
}
}
输出结果:
<?xml version="1.0" encoding="UFT-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>本文出自 “javacareer” 博客,请务必保留此出处http://gaojie.blog.51cto.com/848875/197704
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='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));
}
}
输出结果:
<?xml version="1.0" encoding="UFT-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>本文出自 “javacareer” 博客,请务必保留此出处http://gaojie.blog.51cto.com/848875/197704
相关文章推荐
- string datdString='Wed Oct 12 2016 00:00:00 GMT+0800 (中国标准时间)'将字符串转化为date类型,格式2016-10-12
- JAVA字符串格式化-String.format()的使用 及 Android 中 string.xml 中 字符串格式转化
- [已解决]:通过sql for xml查询到的xml字符串,如何存为标准格式的xml文件?
- 再谈如何以最简单的方法将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
- Android中的string.xml里如何给字符串前后加空格
- c# 关于WCF 接口参数 为 xml 格式的 字符串(string) 的 数据 处理
- String类以及如何将字符串转化为数组,将字符和各种数值返回字符串格式,Integer和int 区别,StringBuilder和StringBuffer区别
- android string.xml中格式资源字符串
- 如何:将 System::String 转换为标准字符串
- 如何将json格式的string字符串转换为string数组
- android 在string.xml 中自定义字符串格式数据
- [转载]C# ToString格式字符串整理(Format)(数字、日期和枚举的标准格式设置说明符)(SamWang)
- 如何:将 System::String 转换为标准字符串
- javascript中字符串格式json如何转化成json对象
- delphi中如何将string类型的字符串数据转化成byte[]字节数组类型的数据
- java中如何将String类型的日期格式为yyyyMMdd转化为date类型的yyyy-MM-dd格式
- Android Studio如何轻松整理字符串到string.xml中
- 【XML】将String格式的XML文件转化成JAVA实体类
- java中String字符串转化成list<Integer>格式
- java如何实现日期格式和字符串之间的转化