`

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

    博客分类:
  • XML
XML 
阅读更多
import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class jdomUtil {
	/**
	 * 将文件写入到xml文件中
	 * 
	 * @param doc
	 * @param filePath
	 * @param encoding
	 */
	public static String StringChangeXML(String str, String encoding) {
		SAXBuilder sb = new SAXBuilder();
		Document doc = null;
		try {
			doc = sb.build(new ByteArrayInputStream(str.getBytes()));
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 将doc中的内容写入文件中
		Format format = Format.getPrettyFormat();// 设置格式
		format.setEncoding(encoding);// 必要,否则无法解析中文
		// 输出为文件,以上操作只是在内存中修改,需要保存为文件来确认修改
		XMLOutputter outp = new XMLOutputter(format);
		return outp.outputString(doc);
	}

	/**
	 * 测试程序文件
	 * 
	 * @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(StringChangeXML(str, "UTF-8"));
	}
}

 输出结果如下:

<?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>

 

由此可见,dom4j能做的jdom也可以!

分享到:
评论

相关推荐

    java解析xml字符串

    1. **初始化XML源**:首先,将XML字符串转化为`StringReader`对象,这是为了将字符串转换为可以被SAXBuilder读取的形式。接着,使用`InputSource`封装`StringReader`,以便SAXBuilder能够正确识别并解析XML数据。 2...

    jdom解析XML文件and把字符串解析成XML

    ### jdom解析XML文件及将字符串解析为XML #### 一、JDOM简介与环境搭建 JDOM(Java Document Object Model)是一种用Java语言编写的轻量级API,用于处理XML文档。它允许开发者轻松地读取、创建、修改XML文档,并将...

    Jdom 生成和解析XML字符串

    这里首先创建了一个 `StringReader` 来读取 XML 字符串,并通过 `InputSource` 将其包装起来,最后使用 `SAXBuilder` 来构建 XML 文档对象模型。 2. **构建文档**: ```java try { Document doc = sax.build...

    dom4j以String的方式读取XML的内容,并给String的值赋值,写入XML中

    在这个场景中,我们关注的是如何使用DOM4J以String的形式读取XML内容,然后将String的值赋给XML中的特定节点,最后将更新后的内容写回XML文件。下面我们将详细探讨这一过程。 1. **解析XML到String** 要将XML文件...

    JDOM读取XML,并且创建XML

    在IT行业中,XML(eXtensible Markup Language)是一种被广泛使用的数据交换格式,尤其在处理结构化数据时。XML文件具有自解释性,使得它容易被人和机器阅读。本篇我们将聚焦于JDOM库,这是一个Java API,专门用于...

    java利用jdom读取xml文件

    5. 保存修改:如果需要保存对XML文件的修改,可以使用`XMLOutputter`类将`Document`对象转换回XML字符串,然后写回文件: ```java XMLOutputter outputter = new XMLOutputter(); String xmlString = outputter....

    jdom解析xml文件

    6. **输出XML**:使用`XMLOutputter`将`Document`对象转换回XML字符串或写入文件。 ```java import org.jdom2.output.XMLOutputter; XMLOutputter xmlOutput = new XMLOutputter(); String xmlString = xmlOutput....

    Jdom解析xml案例

    修改后,需要使用`XMLOutputter`来生成新的XML字符串或写入到文件。 ```java XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.output(document, new FileWriter("output.xml")); ``` ### 示例代码 ...

    JDOM使用详解XML及实例

    - 将Document转换为字符串:`XMLOutputter outputter = new XMLOutputter(); String xmlString = outputter.outputString(doc);` 4. **XPath查询**: - 使用XPath表达式查找元素:`XPath xpath = XPathFactory....

    解析XML所需的jar jdom.jar

    5. **转换回XML字符串**:如果需要将修改后的XML结构转换回字符串,可以使用`XMLOutputter`类: ```java import org.jdom2.output.XMLOutputter; XMLOutputter xmlOutput = new XMLOutputter(); String xmlString =...

    Jdom解析XML

    4. 输出XML,使用`XMLOutputter`类将`Document`对象转换为字符串或写入文件。 **示例代码** ```java import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.jdom2...

    jdom解析xml用到的jar包

    XML(eXtensible Markup Language)是一种用于存储和传输数据的标准格式,特别是在Java应用程序中,XML经常被用来作为数据交换的载体。为了处理XML文档,Java开发者常常使用各种解析器,其中JDOM(Java Document ...

    做一般的XML数据交换过程中,我更乐意传递XML字符

    首先,通过`StringReader`读取XML字符串,然后创建`InputSource`对象,最后用`DocumentBuilder`解析这个输入源得到`Document`。反之,利用`TransformerFactory`和`Transformer`,设置适当的输出属性(如编码),将`...

    XML文件的JDOM解析

    6. **保存到文件**:使用`XMLOutputter`类将修改后的JDOM对象转换回XML字符串,并写入到文件中。 ```java XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); // 设置...

    jdom读写xml说明

    JDOM提供了`XMLOutputter`类来将`Document`对象转换为XML字符串或写入文件。以下是一个写入XML文件的示例: ```java import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.Format; import...

    xml 解析(Jdom代码)

    例如,可以添加新元素、删除元素、修改属性值,然后使用`OutputFormat`和`Transformer`将DOM树转换回XML字符串或写入文件。 **5. 性能与优化** 虽然JDOM提供了方便的API,但其性能相对较低,因为整个XML文档会一次...

    java源码:用jdom解析xml.rar

    2. 加载XML文档:使用`SAXBuilder`类可以将XML字符串或文件加载为一个`Document`对象。例如: ```java import org.jdom2.Document; import org.jdom2.input.SAXBuilder; File xmlFile = new File("path_to_your_xml...

    Jdom架包的使用配置解析xml

    // 输出XML字符串 XMLOutputter outputter = new XMLOutputter(); System.out.println(outputter.outputString(doc)); } } ``` **5. 性能优化** 虽然JDOM在内存占用和性能上优于DOM,但处理大量数据时仍需注意...

    xml解析jdom.jar

    5. **序列化XML**:将内存中的`Document`对象转换回XML字符串或写入文件,使用`XMLOutputter`类。例如: ```java XMLOutputter outputter = new XMLOutputter(); outputter.output(document, new ...

Global site tag (gtag.js) - Google Analytics