最近,媳妇要做一个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>

转载于:https://blog.51cto.com/gaojie/197704

如何将xml的String字符串转化标准格式的String字符串相关推荐

  1. python把字符串转化为字典_python 将字符串转换为字典

    在一般的工程处理中,需要将获取的字符串数据转换为字典,这样处理起来会非常方便. 我获取的是json数据: content = {"corpus_no":"64702772 ...

  2. python字符串转化列表_Python列表到字符串的转换

    python字符串转化列表 Sometimes we want to convert the list to a string so that we can print it or log it fo ...

  3. java 将字符串转化成输入流_JAVA将字符串变为输入流

    JAVA将字符串变为输入流 关于字符串转化为输入流,我找到2种方法: 用StringReader将字符串转化为Reader 用ByteArrayInputStream将字符串转化为InputStrea ...

  4. java字符串转化为数组_Go 语言字符串和数组转化 | 臭大佬

    代码 package main import ( "fmt" "strconv" ) func main() { str := "17" i ...

  5. python把字符串转化为字典_python 将字符串转换成字典dict的各种方式总结

    1)利用eval可以将字典格式的字符串与字典户转 >>>mstr = '{"name":"yct","age":10}' ...

  6. c 语言 string类型转换,用标准c++实现string与各种类型之间的转换

    要实现这个目标,非stringstream类莫属. 这个类在头文件中定义, < sstream>库定义了三种类:istringstream.ostringstream和stringstre ...

  7. php 字符串转化字符集,php convert_cyr_string 将字符串由一种 Cyrillic 字符集转换成另一种...

    convert_cyr_string - 将字符由一种 Cyrillic 字符转换成另一种 convert_cyr_string 函数基本语法介绍: convert_cyr_string(str,fr ...

  8. 将字符串转化为指定长度的字符串

     http://cn2.php.net/manual/zh/function.str-pad.php

  9. php将字符串转换为json格式,js中将字符串转换为json格式的三种方法

    json在js的开发过程中经常会用到,像在使用ajax开发的项目过程中,经常需要将json格式的字符串返回到前端,前端解析成json对象. 下面为大家介绍下将字符串转换为json对象的三种常用的方法: ...

最新文章

  1. php的框架目录,Laravel 框架目录结构
  2. 创建WCF第一个应用程序
  3. Python基础(14)_python模块之configparser模块、suprocess
  4. 基本操作:win10系统磁盘分区
  5. 基于SegNet和UNet的遥感图像分割代码解读
  6. 三种常见单片机时钟电路方案,对比其优缺点
  7. OpenCV-Python bindings是如何生成的(1)
  8. 怎样在数据绑定到DATAGRID之前,先判断其中一个字段是否为空?
  9. AIR中文帮助 第十章. 窗体(Windows)和菜单
  10. 随想录(在x86 linux上仿真多核cpu运行)
  11. Python基础知识汇总
  12. php 开发商城 注意,开发PHP商城要注意的一些常见安全问题
  13. Arduboy 游戏机制作参考教程
  14. .NET Interop.SHDocVw和MSHTML引用如何操作
  15. pcb怎么画边框_PCB设计--PCB画图技巧
  16. 苹果电脑u盘装win7系统教程
  17. js实现上传图片之后回显
  18. 论文引用图片时的版权声明(Reproduced with permission, courtesy of [copyright owner])
  19. CSS3中的元素过渡属性transition
  20. 苹果宣布换芯 背后究竟硬气何在?

热门文章

  1. MKCMS6.2.3视频程序源码修复列表页
  2. Echo:新生好看的一言网站源码
  3. Window Services的调试和非托管dll的引用及其他一些注意问题
  4. jQuery倒计时(仿团购)
  5. Moodle: 查询 / 更新 / 添加 / 删除 / 导出 用户 ($DB用法)
  6. 在shell中如何判断一个变量是否为空
  7. Linux 性能监测
  8. 深入理解Magento – 第三章 – 布局,块和模板
  9. shell数值运算的方法
  10. eclipse 取消自动括号补全