Java实现XML格式化

简介: Java实现XML格式化

Java可以使用自带的API将XML格式化,直接贴上工具类:

/**
* 格式化xml
*
* @param xmlString xml内容
* @param indent 向前缩进多少空格
* @param ignoreDeclaration 是否忽略描述
* @return 格式化后的xml
*/
public static String prettyPrintByTransformer(String xmlString, int indent, boolean ignoreDeclaration) {
   try {
       InputSource src = new InputSource(new StringReader(xmlString));
       Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);
       TransformerFactory transformerFactory = TransformerFactory.newInstance();
       transformerFactory.setAttribute("indent-number", indent);
       Transformer transformer = transformerFactory.newTransformer();
       transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
       transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ignoreDeclaration ? "yes" : "no");
       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
       Writer out = new StringWriter();
       transformer.transform(new DOMSource(document), new StreamResult(out));
       return out.toString();
   } catch (Exception e) {
       throw new RuntimeException("Error occurs when pretty-printing xml:\n" + xmlString, e);
   }
}

测试:

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
public class Test {
    public static void main(String[] args) throws TransformerConfigurationException {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\n" +
                "<!--Autogenerated by Cloudera Manager-->\n" +
                "<configuration><property><name>yarn.acl.enable</name><value>true</value></property><property><name>yarn.admin.acl</name><value>*</value></property></configuration>\n";
        System.out.println(prettyPrintByTransformer(xml, 2, false));
    }
}


目录
相关文章
|
2月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
11 0
|
5月前
|
XML 数据采集 JavaScript
Java【XML 配置文件解析】
Java【XML 配置文件解析】
|
5月前
|
存储 Java 测试技术
JAVA-MAVEN初学者教程(配置、pom.xml、依赖管理等)
JAVA-MAVEN初学者教程(配置、pom.xml、依赖管理等)
247 0
|
4月前
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
58 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
22天前
|
XML JSON JavaScript
Java中XML和JSON的比较与应用指南
本文对比了Java中XML和JSON的使用,XML以自我描述性和可扩展性著称,适合结构复杂、需验证的场景,但语法冗长。JSON结构简洁,适用于轻量级数据交换,但不支持命名空间。在Java中,处理XML可使用DOM、SAX解析器或XPath,而JSON可借助GSON、Jackson库。根据需求选择合适格式,注意安全、性能和可读性。
28 0
|
22天前
|
XML JavaScript Java
如何去除Java dom生成的xml文件头的standalone=“no“ ,去掉后无换行
如何去除Java dom生成的xml文件头的standalone=“no“ ,去掉后无换行
10 0
|
27天前
|
XML JSON JavaScript
使用JSON和XML:数据交换格式在Java Web开发中的应用
【4月更文挑战第3天】本文比较了JSON和XML在Java Web开发中的应用。JSON是一种轻量级、易读的数据交换格式,适合快速解析和节省空间,常用于API和Web服务。XML则提供更强的灵活性和数据描述能力,适合复杂数据结构。Java有Jackson和Gson等库处理JSON,JAXB和DOM/SAX处理XML。选择格式需根据应用场景和需求。
|
2月前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0
|
2月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
154 1
|
3月前
|
Java
DecimalFormat(Java中的应用——十进制数字格式化)
DecimalFormat(Java中的应用——十进制数字格式化)
154 0