java this.formatexml_Java XML格式化,文件到XML,XML到文档

扩展标记语言(XML)是不同应用程序之间的消息传递和通信的流行媒介之一。 由于XML是开源的,并且通过DTD和XSD提供对数据格式的控制,因此它广泛用于各种技术。

Java XML格式化程序

第三方API将Document对象和XML消息作为String返回,这里编写了一个简单的XmlFormatter类来使用适当的缩进格式化XML并将Document对象转换为XML String。

package com.test.java.xmlutil;

import org.apache.xml.serialize.OutputFormat;

import org.apache.xml.serialize.XMLSerializer;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import java.io.IOException;

import java.io.StringReader;

import java.io.StringWriter;

import java.io.Writer;

/**

* Utility Class for formatting XML

* @author Pankaj

*

*/

public class XmlFormatter {

/**

*

* @param unformattedXml - XML String

* @return Properly formatted XML String

*/

public String format(String unformattedXml) {

try {

Document document = parseXmlFile(unformattedXml);

OutputFormat format = new OutputFormat(document);

format.setLineWidth(65);

format.setIndenting(true);

format.setIndent(2);

Writer out = new StringWriter();

XMLSerializer serializer = new XMLSerializer(out format);

serializer.serialize(document);

return out.toString();

} catch (IOException e) {

e.printStackTrace();

return "";

}

}

/**

* This function converts String XML to Document object

* @param in - XML String

* @return Document object

*/

private Document parseXmlFile(String in) {

try {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource(new StringReader(in));

return db.parse(is);

} catch (ParserConfigurationException e) {

throw new RuntimeException(e);

} catch (SAXException e) {

throw new RuntimeException(e);

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* Takes an XML Document object and makes an XML String. Just a utility

* function.

*

* @param doc - The DOM document

* @return the XML String

*/

public String makeXMLString(Document doc) {

String xmlString = "";

if (doc != null) {

try {

TransformerFactory transfac = TransformerFactory.newInstance();

Transformer trans = transfac.newTransformer();

trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION "yes");

trans.setOutputProperty(OutputKeys.INDENT "yes");

StringWriter sw = new StringWriter();

StreamResult result = new StreamResult(sw);

DOMSource source = new DOMSource(doc);

trans.transform(source result);

xmlString = sw.toString();

} catch (Exception e) {

e.printStackTrace();

}

}

return xmlString;

}

public static void main(String args[]){

XmlFormatter formatter = new XmlFormatter();

String book = "<?xml version=\"1.0\"?>Gambardella MatthewXML Developers GuideComputer44.952000-10-01An in-depth look at creating applications with XML.Ralls KimMidnight RainFantasy5.952000-12-16A former architect battles corporate zombies an evil sorceress and her own childhood to become queen of the world.";

System.out.println(formatter.format(book));

}

}

要使用此类,需要Apache xercesImpl.jar,可以从他们的网站下载。上述类的输出是格式正确的XML字符串:

Gambardella Matthew

XML Developers Guide

Computer

44.95

2000-10-01

An in-depth look at creating applications with XML.

Ralls Kim

Midnight Rain

Fantasy

5.95

2000-12-16

A former architect battles corporate zombies an evil

sorceress and her own childhood to become queen of the world.

这个实用程序类用于在java中格式化xml并将XML转换为Document,反之亦然。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值