一 DOM 以下代码用DOM是生成一个XML文档
package xml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class XMLHandler { public void createXML() throws FileNotFoundException { Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbf.newDocumentBuilder(); doc = docBuilder.newDocument(); Element root = doc.createElement("root"); doc.appendChild(root); Element country = doc.createElement("contry"); country.appendChild(doc.createTextNode("China")); root.appendChild(country); Element city = doc.createElement("city"); city.appendChild(doc.createTextNode("Beijing")); country.appendChild(city); city = doc.createElement("city"); city.appendChild(doc.createTextNode("Shanghai")); country.appendChild(city); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); File file = new File("E://cities.xml"); FileOutputStream out = new FileOutputStream(file); StreamResult xmlResult = new StreamResult(out); transformer.transform(new DOMSource(doc), xmlResult); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); throw e; } } public static void main(String args[]) { XMLHandler xh = new XMLHandler(); try { xh.createXML(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
生成的xml如下
<?xml version="1.0" encoding="UTF-8"?><country><china><city>Beijing</city><city>Shanghai</city></china></country>
xml所有内容都在一行里面,没有换行和缩进
现在在代码
Transformer transformer = tf.newTransformer();
后面加上
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
再次生成xml
<?xml version="1.0" encoding="UTF-8"?> <country> <china> <city>Beijing</city> <city>Shanghai</city> </china> </country>
这次有了换行,但是仍然没有缩进。为此我查找了多次google和百度都没有找到解决方法,可能是有或者自己没耐心,就想了办法来处理。在前面加空格
Element.appendChild(doc.createTextNode("/n "));
最后代码为
package xml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; 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.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class XMLHandler { public void createXML() throws FileNotFoundException { Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbf.newDocumentBuilder(); doc = docBuilder.newDocument(); Element country = doc.createElement("country"); doc.appendChild(country); country.appendChild(doc.createTextNode("/n ")); Element china = doc.createElement("china"); country.appendChild(china); china.appendChild(doc.createTextNode("/n ")); Element city = doc.createElement("city"); city.appendChild(doc.createTextNode("Beijing")); china.appendChild(city); china.appendChild(doc.createTextNode("/n ")); city = doc.createElement("city"); city.appendChild(doc.createTextNode("Shanghai")); china.appendChild(city); china.appendChild(doc.createTextNode("/n ")); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); File file = new File("E://cities.xml"); FileOutputStream out = new FileOutputStream(file); StreamResult xmlResult = new StreamResult(out); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(doc), xmlResult); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); throw e; } } public static void main(String args[]) { XMLHandler xh = new XMLHandler(); try { xh.createXML(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
最终生成的xml为
<?xml version="1.0" encoding="UTF-8"?> <country> <china> <city>Beijing</city> <city>Shanghai</city> </china> </country>
二 SAX 用SAX生成xml并换行缩进
package xml; import java.io.FileOutputStream; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; public class XMLHandler { public String createXMLFile() { String xmlStr = null; try { Result resultXml = new StreamResult(new FileOutputStream("E://cities.xml")); StringWriter writerStr = new StringWriter(); SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler th = sff.newTransformerHandler(); Transformer transformer = th.getTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); th.setResult(resultXml); th.startDocument(); AttributesImpl attr = new AttributesImpl(); th.startElement("", "", "country", attr); th.startElement("", "", "china", attr); th.startElement("", "", "city", attr); String bj = "Beijing"; th.characters(bj.toCharArray(), 0, bj.length()); th.endElement("", "", "city"); th.startElement("", "", "city", attr); String sh = "Shanghai"; th.characters(sh.toCharArray(), 0, sh.length()); th.endElement("", "", "city"); th.endElement("", "", "china"); th.endElement("", "", "country"); th.endDocument(); xmlStr = writerStr.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return xmlStr; } public static void main(String args[]) { XMLHandler xh = new XMLHandler(); xh.createXMLFile(); } }
用SAX生成的xml如下
<?xml version="1.0" encoding="UTF-8"?><country><china><city>Beijing</city><city>Shanghai</city></china></country>
同样没有换行和缩进
在代码中增加
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
后生成的xml有了换行但仍没有缩进
<?xml version="1.0" encoding="UTF-8"?> <country> <china> <city>Beijing</city> <city>Shanghai</city> </china> </country>
同样按照DOM中的方法加空格
String four = "/n "; TransformerHandler.characters(four.toCharArray(),0,four.length());
最终SAX代码如下:
package xml; import java.io.FileOutputStream; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; public class XMLHandler { public String createXMLFile() { String xmlStr = null; try { Result resultXml = new StreamResult(new FileOutputStream("E://cities.xml")); StringWriter writerStr = new StringWriter(); SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler th = sff.newTransformerHandler(); Transformer transformer = th.getTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); th.setResult(resultXml); th.startDocument(); String four = "/n "; String eight = "/n "; AttributesImpl attr = new AttributesImpl(); th.startElement("", "", "country", attr); th.characters(four.toCharArray(),0,four.length()); th.startElement("", "", "china", attr); th.characters(eight.toCharArray(),0,eight.length()); th.startElement("", "", "city", attr); String bj = "Beijing"; th.characters(bj.toCharArray(), 0, bj.length()); th.endElement("", "", "city"); th.characters(eight.toCharArray(),0,eight.length()); th.startElement("", "", "city", attr); String sh = "Shanghai"; th.characters(sh.toCharArray(), 0, sh.length()); th.endElement("", "", "city"); th.characters(four.toCharArray(),0,four.length()); th.endElement("", "", "china"); th.endElement("", "", "country"); th.endDocument(); xmlStr = writerStr.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return xmlStr; } public static void main(String args[]) { XMLHandler xh = new XMLHandler(); xh.createXMLFile(); } }
生成的XML
<?xml version="1.0" encoding="UTF-8"?> <country> <china> <city>Beijing</city> <city>Shanghai</city> </china> </country>