一 DOM 以下代碼用DOM是產生一個XML文檔
package xml;</p><p>import java.io.File;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;</p><p>import javax.xml.parsers.DocumentBuilder;<br />import javax.xml.parsers.DocumentBuilderFactory;<br />import javax.xml.parsers.ParserConfigurationException;<br />import javax.xml.transform.Transformer;<br />import javax.xml.transform.TransformerConfigurationException;<br />import javax.xml.transform.TransformerException;<br />import javax.xml.transform.TransformerFactory;<br />import javax.xml.transform.dom.DOMSource;<br />import javax.xml.transform.stream.StreamResult;</p><p>import org.w3c.dom.Document;<br />import org.w3c.dom.Element;</p><p>public class XMLHandler {<br />public void createXML() throws FileNotFoundException {<br />Document doc = null;<br />DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();</p><p>try {<br />DocumentBuilder docBuilder = dbf.newDocumentBuilder();<br />doc = docBuilder.newDocument();<br />Element root = doc.createElement("root");<br />doc.appendChild(root);</p><p>Element country = doc.createElement("contry");<br />country.appendChild(doc.createTextNode("China"));<br />root.appendChild(country);</p><p>Element city = doc.createElement("city");<br />city.appendChild(doc.createTextNode("Beijing"));<br />country.appendChild(city);</p><p>city = doc.createElement("city");<br />city.appendChild(doc.createTextNode("Shanghai"));<br />country.appendChild(city);</p><p>TransformerFactory tf = TransformerFactory.newInstance();<br />Transformer transformer = tf.newTransformer();<br />File file = new File("E://cities.xml");<br />FileOutputStream out = new FileOutputStream(file);<br />StreamResult xmlResult = new StreamResult(out);<br />transformer.transform(new DOMSource(doc), xmlResult);<br />} catch (ParserConfigurationException e) {<br />e.printStackTrace();<br />} catch (TransformerConfigurationException e) {<br />e.printStackTrace();<br />} catch (TransformerException e) {<br />e.printStackTrace();<br />} catch (FileNotFoundException e) {<br />e.printStackTrace();<br />throw e;<br />}<br />}</p><p>public static void main(String args[]) {<br />XMLHandler xh = new XMLHandler();<br />try {<br />xh.createXML();<br />} catch (FileNotFoundException e) {<br />e.printStackTrace();<br />}<br />}<br />}<br />
產生的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"?><br /><country><br /><china><br /><city>Beijing</city><br /><city>Shanghai</city><br /></china><br /></country>
這次有了換行,但是仍然沒有縮排。為此我尋找了多次google和百度都沒有找到解決方案,可能是有或者自己沒耐心,就想了辦法來處理。在前面加空格
Element.appendChild(doc.createTextNode("/n "));
最後代碼為
package xml;</p><p>import java.io.File;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;</p><p>import javax.xml.parsers.DocumentBuilder;<br />import javax.xml.parsers.DocumentBuilderFactory;<br />import javax.xml.parsers.ParserConfigurationException;<br />import javax.xml.transform.OutputKeys;<br />import javax.xml.transform.Transformer;<br />import javax.xml.transform.TransformerConfigurationException;<br />import javax.xml.transform.TransformerException;<br />import javax.xml.transform.TransformerFactory;<br />import javax.xml.transform.dom.DOMSource;<br />import javax.xml.transform.stream.StreamResult;</p><p>import org.w3c.dom.Document;<br />import org.w3c.dom.Element;</p><p>public class XMLHandler {<br />public void createXML() throws FileNotFoundException {<br />Document doc = null;<br />DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();</p><p>try {<br />DocumentBuilder docBuilder = dbf.newDocumentBuilder();<br />doc = docBuilder.newDocument();</p><p>Element country = doc.createElement("country");</p><p>doc.appendChild(country);</p><p>country.appendChild(doc.createTextNode("/n "));</p><p>Element china = doc.createElement("china");<br />country.appendChild(china);</p><p>china.appendChild(doc.createTextNode("/n "));</p><p>Element city = doc.createElement("city");<br />city.appendChild(doc.createTextNode("Beijing"));<br />china.appendChild(city);</p><p>china.appendChild(doc.createTextNode("/n "));</p><p>city = doc.createElement("city");<br />city.appendChild(doc.createTextNode("Shanghai"));<br />china.appendChild(city);</p><p>china.appendChild(doc.createTextNode("/n "));</p><p>TransformerFactory tf = TransformerFactory.newInstance();<br />Transformer transformer = tf.newTransformer();<br />File file = new File("E://cities.xml");<br />FileOutputStream out = new FileOutputStream(file);<br />StreamResult xmlResult = new StreamResult(out);<br />transformer.setOutputProperty(OutputKeys.INDENT, "yes");<br />transformer.transform(new DOMSource(doc), xmlResult);<br />} catch (ParserConfigurationException e) {<br />e.printStackTrace();<br />} catch (TransformerConfigurationException e) {<br />e.printStackTrace();<br />} catch (TransformerException e) {<br />e.printStackTrace();<br />} catch (FileNotFoundException e) {<br />e.printStackTrace();<br />throw e;<br />}<br />}</p><p>public static void main(String args[]) {<br />XMLHandler xh = new XMLHandler();<br />try {<br />xh.createXML();<br />} catch (FileNotFoundException e) {<br />e.printStackTrace();<br />}<br />}<br />}<br />
最終產生的xml為
<?xml version="1.0" encoding="UTF-8"?><br /><country><br /> <china><br /> <city>Beijing</city><br /> <city>Shanghai</city><br /> </china><br /></country>
二 SAX 用SAX產生xml並換行縮排
package xml;</p><p>import java.io.FileOutputStream;<br />import java.io.StringWriter;</p><p>import javax.xml.transform.OutputKeys;<br />import javax.xml.transform.Result;<br />import javax.xml.transform.Transformer;<br />import javax.xml.transform.TransformerConfigurationException;<br />import javax.xml.transform.sax.SAXTransformerFactory;<br />import javax.xml.transform.sax.TransformerHandler;<br />import javax.xml.transform.stream.StreamResult;</p><p>import org.xml.sax.SAXException;<br />import org.xml.sax.helpers.AttributesImpl;</p><p>public class XMLHandler {</p><p>public String createXMLFile() {<br />String xmlStr = null;<br />try {<br />Result resultXml = new StreamResult(new FileOutputStream("E://cities.xml"));<br />StringWriter writerStr = new StringWriter();<br />SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();<br />TransformerHandler th = sff.newTransformerHandler();<br />Transformer transformer = th.getTransformer();<br />transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");<br />th.setResult(resultXml);<br />th.startDocument();<br />AttributesImpl attr = new AttributesImpl();<br />th.startElement("", "", "country", attr);<br />th.startElement("", "", "china", attr);</p><p>th.startElement("", "", "city", attr);<br />String bj = "Beijing";<br />th.characters(bj.toCharArray(), 0, bj.length());<br />th.endElement("", "", "city");</p><p>th.startElement("", "", "city", attr);<br />String sh = "Shanghai";<br />th.characters(sh.toCharArray(), 0, sh.length());<br />th.endElement("", "", "city");</p><p>th.endElement("", "", "china");<br />th.endElement("", "", "country");<br />th.endDocument();<br />xmlStr = writerStr.getBuffer().toString();<br />} catch (TransformerConfigurationException e) {<br />e.printStackTrace();<br />} catch (SAXException e) {<br />e.printStackTrace();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />return xmlStr;<br />}</p><p>public static void main(String args[]) {<br />XMLHandler xh = new XMLHandler();<br />xh.createXMLFile();<br />}<br />}<br />
用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"?><br /><country><br /><china><br /><city>Beijing</city><br /><city>Shanghai</city><br /></china><br /></country><br />
同樣按照DOM中的方法加空格
String four = "/n ";<br />TransformerHandler.characters(four.toCharArray(),0,four.length());
最終SAX代碼如下:
package xml;</p><p>import java.io.FileOutputStream;<br />import java.io.StringWriter;</p><p>import javax.xml.transform.OutputKeys;<br />import javax.xml.transform.Result;<br />import javax.xml.transform.Transformer;<br />import javax.xml.transform.TransformerConfigurationException;<br />import javax.xml.transform.sax.SAXTransformerFactory;<br />import javax.xml.transform.sax.TransformerHandler;<br />import javax.xml.transform.stream.StreamResult;</p><p>import org.xml.sax.SAXException;<br />import org.xml.sax.helpers.AttributesImpl;</p><p>public class XMLHandler {</p><p>public String createXMLFile() {<br />String xmlStr = null;<br />try {<br />Result resultXml = new StreamResult(new FileOutputStream("E://cities.xml"));<br />StringWriter writerStr = new StringWriter();<br />SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();<br />TransformerHandler th = sff.newTransformerHandler();<br />Transformer transformer = th.getTransformer();<br />transformer.setOutputProperty(OutputKeys.INDENT, "yes");<br />transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");<br />th.setResult(resultXml);<br />th.startDocument();<br />String four = "/n ";<br />String eight = "/n ";<br />AttributesImpl attr = new AttributesImpl();</p><p>th.startElement("", "", "country", attr);</p><p>th.characters(four.toCharArray(),0,four.length());</p><p>th.startElement("", "", "china", attr);</p><p>th.characters(eight.toCharArray(),0,eight.length());</p><p>th.startElement("", "", "city", attr);<br />String bj = "Beijing";<br />th.characters(bj.toCharArray(), 0, bj.length());<br />th.endElement("", "", "city");</p><p>th.characters(eight.toCharArray(),0,eight.length());</p><p>th.startElement("", "", "city", attr);<br />String sh = "Shanghai";<br />th.characters(sh.toCharArray(), 0, sh.length());<br />th.endElement("", "", "city");</p><p>th.characters(four.toCharArray(),0,four.length());</p><p>th.endElement("", "", "china");<br />th.endElement("", "", "country");<br />th.endDocument();<br />xmlStr = writerStr.getBuffer().toString();<br />} catch (TransformerConfigurationException e) {<br />e.printStackTrace();<br />} catch (SAXException e) {<br />e.printStackTrace();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />return xmlStr;<br />}</p><p>public static void main(String args[]) {<br />XMLHandler xh = new XMLHandler();<br />xh.createXMLFile();<br />}<br />}<br />
產生的XML
<?xml version="1.0" encoding="UTF-8"?><br /><country><br /> <china><br /> <city>Beijing</city><br /> <city>Shanghai</city><br /> </china><br /></country>