DomBuilding.java
- import org.apache.xalan.serialize.SerializerToXML;
- import org.apache.xalan.templates.OutputProperties;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import java.io.File;
- import java.io.StringWriter;
- import java.util.Properties;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- public class DomBuilding
- {
- static Document document;
- public DomBuilding()
- {
- }
- public Element addElement(Document document, Element parent,
- String elementname) throws Exception
- {
- Element temp = document.createElement(elementname);
- parent.appendChild(temp);
- return temp;
- }
- public Document newDom() throws Exception
- {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- document = builder.newDocument();
- return document;
- }
- public void transformDomToXml(Document document, String FileName)
- throws Exception
- {
- TransformerFactory tFactory = TransformerFactory.newInstance();
- Transformer transformer = tFactory.newTransformer();
- DOMSource source = new DOMSource(document);
- StreamResult result = new StreamResult(new File(FileName));
- transformer.transform(source, result);
- }
- public String transformDomToOutputStream(Document document)
- throws Exception
- {
- StringWriter sw = new StringWriter();
- String result = new String();
- SerializerToXML serxml = new SerializerToXML();
- serxml.setWriter(sw);
- Properties properties = OutputProperties
- .getDefaultMethodProperties("xml");
- properties.setProperty("encoding", "GBK");
- serxml.setOutputFormat(properties);
- serxml.serialize(document);
- serxml.flushWriter();
- result = sw.toString();
- return result;
- }
- }
BookBean.java
- public class BookBean {
- private String name;
- private String prise;
- private String author;
- private String press;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPrise() {
- return prise;
- }
- public void setPrise(String prise) {
- this.prise = prise;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getPress() {
- return press;
- }
- public void setPress(String press) {
- this.press = press;
- }
- }
构建类:
BeanTOXML.java
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Text;
- public class BeanTOXML {
- public String buildXML(BookBean bean) {
- String returnXML = "";
- if (bean != null) {
- try {
- Document document = null;
- DomBuilding domBuilding = new DomBuilding();
- document = domBuilding.newDom();
- //构建根节点
- Element root = document.createElement("books");
- document.appendChild(root);
- Element bookEL = domBuilding.addElement(document, root, "book");
- if (bean.getName() != null) {
- Element name = domBuilding.addElement(document, bookEL,
- "name");
- Text idText = document.createTextNode(bean.getName());
- name.appendChild(idText);
- }
- if (bean.getPrise() != null) {
- Element prise = domBuilding.addElement(document, bookEL,
- "prise");
- Text idText = document.createTextNode(bean.getPrise());
- prise.appendChild(idText);
- }
- if (bean.getAuthor() != null) {
- Element author = domBuilding.addElement(document, bookEL,
- "author");
- Text idText = document.createTextNode(bean.getAuthor());
- author.appendChild(idText);
- }
- if (bean.getPress() != null) {
- Element press = domBuilding.addElement(document, bookEL,
- "press");
- Text idText = document.createTextNode(bean.getPress());
- press.appendChild(idText);
- }
- //把生成的document转换成String形式
- returnXML = domBuilding.transformDomToOutputStream(document);
- } catch (Exception e) {
- System.out.println("要解析的bean为空!");
- }
- }
- return returnXML;
- }
- public static void main(String[] args) {
- BookBean bean = new BookBean();
- bean.setName("bookName");
- bean.setPrise("99");
- bean.setAuthor("bookAuthor");
- bean.setPress("bookPress");
- String toXML = new BeanTOXML().buildXML(bean);
- System.out.println(toXML);
- }
- }
输出结果:
- <?xml version="1.0" encoding="GBK" standalone="no"?>
- <books><book><name>bookName</name><prise>99</prise><author>bookAuthor</author><press>bookPress</press></book></books>