北天软件工作室
北天软件集网站建设、网站开发、软件开发、网站优化SEO、网站宣传、网站开发成一体的网络公司。北天软件是专业的网站建设、网站开发、设计、制作和网站国际推广、搜索引擎推广的网络公司。口号:考虑企业所需,实现企业所想。JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!   旧版java技术网 | 设为首页 | 文章搜索 | RSS订阅地图
免费使用JavaCMS自助建站系统
  文章搜索:   
初学者园地  javascript  java技术  .Net技术 XML/WebService  数据库技术  web2.0技术  设计模式  设计框架  SEO技术  综合知识
您现在的位置是: 北天软件门户网>>java技术>>详细信息
如何将xml的String字符串转化标准格式的String字符串
        最近,媳妇要做一个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>
关闭窗口 】   【 返回首页
推荐文章
· Domino中使用Lotus Sc...
· XML和Java: 低级或高...
· 五个免费UML建模工具...
· NetBeans 6.8 Beta发...
· MyFaces Core v1.2.8...
· 实战 SSH 端口转发
· 使用JBuilder和WTK2....
· 从程序员到CTO
· 什么是真正的实时操...
· Eclipse 3.4使用摘录
· "struts中文问题","s...
· Redhat9下的java中文...
· J2SE综合:浅析Java语...
· HttpURLConnection简...
· Java中serialVersionU...
· Map、Set、Iterator迭...
· Eclipse 也推出了应...
· Apache 1.3 最终版发布
· 前高管反思:微软企业...
· Oracle 公布 Java 发...
北天软件工作室 粤ICP备06079815号 版权所有©2006-2008
精彩出品 JavaCMS自助建站 (C)2006-2008 www.it3838.com limited.all rights reserved.
Powered by JavaCMS V2.6.0