XML文件和DOM DocumentString字符串三种类型之间的相互转换

Posted FightingDeng2022

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML文件和DOM DocumentString字符串三种类型之间的相互转换相关的知识,希望对你有一定的参考价值。

    最近在工作中用到了解析XML文件,网上的教程一言难尽,分享一个我自己解析XML文件的方法,记录一下。有XML文件转成DOM,XML文件转成Sting,String转DOM等各种操作。用的包是 org.w3c.dom.Document下的

/**
* @Author dengzp
* @Date 2022/3/22
* @Version 1.0
* @Description
*/
public class OperateXMLByDOM 
/**
* 将给定文件的内容或者给定 URI 的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象
*
* @param filePath 文件所在路径
* @return DOM的Document对象
* @throws Exception
*/
public static Document xml2Doc(String filePath)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
FileInputStream inputStream = null;
Document doc = null;
try
builder = factory.newDocumentBuilder();

/* 通过文件方式读取,注意文件保存的编码和文件的声明编码要一致(默认文件声明是UTF-8) */
File file = new File(filePath);
doc = builder.parse(file);

/* 通过一个URL方式读取 */
URI uri = new URI(filePath);
doc = builder.parse(uri.toString());

/* 通过java IO 流的读取 */
inputStream = new FileInputStream(filePath);
doc = builder.parse(inputStream);
return doc;
catch (Exception e)
return null;
finally
if (inputStream != null)
try
inputStream.close();
catch (IOException e)
return null;





/**
* Document 转换为 String 并且进行了格式化缩进
*
* @param doc XML的Document对象
* @return String
*/
public static String doc2FormatString(Document doc)
StringWriter stringWriter = null;
try
stringWriter = new StringWriter();
if (doc != null)
OutputFormat format = new OutputFormat(doc, "UTF-8", true);
//format.setIndenting(true);//设置是否缩进,默认为true
//format.setIndent(4);//设置缩进字符数
//format.setPreserveSpace(false);//设置是否保持原来的格式,默认为 false
//format.setLineWidth(500);//设置行宽度
XMLSerializer serializer = new XMLSerializer(stringWriter, format);
serializer.asDOMSerializer();
serializer.serialize(doc);
return stringWriter.toString();
else
return null;

catch (Exception e)
return null;
finally
if (stringWriter != null)
try
stringWriter.close();
catch (IOException e)
return null;





/**
* Document 转换为 String
*
* @param doc XML的Document对象
* @return String
*/
public static String doc2String(Document doc)
try
Source source = new DOMSource(doc);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
return stringWriter.getBuffer().toString();
catch (Exception e)
return null;



/**
* String 转换为 Document 对象
*
* @param xml 字符串
* @return Document 对象
*/
public static Document string2Doc(String xml)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
InputSource source = null;
StringReader reader = null;
try
builder = factory.newDocumentBuilder();
reader = new StringReader(xml);
source = new InputSource(reader);//使用字符流创建新的输入源
doc = builder.parse(source);
return doc;
catch (Exception e)
return null;
finally
if (reader != null)
reader.close();




/**
* Document 保存为 XML 文件
*
* @param doc Document对象
* @param path 文件路径
*/
public static void doc2XML(Document doc, String path)
try
Source source = new DOMSource(doc);
Result result = new StreamResult(new File(path));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
catch (Exception e)
return;



public static void main(String[] args)
Document doc = xml2Doc("/Users/dengzhaoping/Desktop/license.xml");
System.out.println("aa" + doc);
System.out.println("bb" + doc2String(doc));
System.out.println("cc" + doc2FormatString(doc));
doc = string2Doc(doc2FormatString(doc));
doc2XML(doc, "1.xml");

以上是关于XML文件和DOM DocumentString字符串三种类型之间的相互转换的主要内容,如果未能解决你的问题,请参考以下文章

使用dom4j创建和解析xml文件

Java web——xml文件读取的解析方式(DOM和SAX)

XML文件解析DOM解析和SAX解析

使用 stax 和 dom 读取大型 XML 文件

Android之DOM解析XML

Android之DOM解析XML