生成XML文档
Posted 牧云人生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成XML文档相关的知识,希望对你有一定的参考价值。
JAVA操作XML文档主要有四种方式,分别是DOM、SAX、JDOM和DOM4J,DOM和SAX是官方提供的,而JDOM和DOM4J则是引用第三方库的,其中用的最多的是DOM4J方式。运行效率和内存使用方面最优的是SAX,但是由于SAX是基于事件的方式,所以SAX无法在编写XML的过程中对已编写内容进行修改,但对于不用进行频繁修改的需求,还是应该选择使用SAX。
现在来生成这样的一份XML文档,文档内容如下:
<?xml version="1.0" encoding="utf-8" ?> <bookList> <book id="one"> <name>one book</name> <year>2017</year> <author>tom</author> </book> <book id="two"> <name>two book</name> <year>2018</year> <author>jim</author> </book> </bookList>
第一,使用DOM的方式生成。
1 package xmls; 2 import org.w3c.dom.Document; 3 import org.w3c.dom.Element; 4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 import javax.xml.parsers.ParserConfigurationException; 7 import javax.xml.transform.OutputKeys; 8 import javax.xml.transform.Transformer; 9 import javax.xml.transform.TransformerException; 10 import javax.xml.transform.TransformerFactory; 11 import javax.xml.transform.dom.DOMSource; 12 import javax.xml.transform.stream.StreamResult; 13 import java.io.File; 14 /** 15 * Created by lenovo on 2017-6-3. 16 */ 17 public class DOMDemo { 18 public static void main(String[] args){ 19 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 20 try { 21 DocumentBuilder db = dbf.newDocumentBuilder(); 22 //创建文档 23 Document document = db.newDocument(); 24 //设置Standalone为no 25 document.setXmlStandalone(true); 26 //生成根节点 27 Element bookList = document.createElement("bookList"); 28 //生成第一本书 29 Element bookOne = document.createElement("book"); 30 bookOne.setAttribute("id", "one"); 31 //生成第一本书的名字 32 Element nameOne = document.createElement("name"); 33 nameOne.setTextContent("one book"); 34 bookOne.appendChild(nameOne); 35 //生成第一本书的年份 36 Element yearOne = document.createElement("year"); 37 yearOne.setTextContent("2017"); 38 bookOne.appendChild(yearOne); 39 //生成第一本书的作者 40 Element authorOne = document.createElement("author"); 41 authorOne.setTextContent("tom"); 42 bookOne.appendChild(authorOne); 43 //生成第二本书 44 Element bookTwo = document.createElement("book"); 45 bookTwo.setAttribute("id", "two"); 46 //生成第二本书的名字 47 Element nameTwo = document.createElement("name"); 48 nameTwo.setTextContent("two book"); 49 bookTwo.appendChild(nameTwo); 50 //生成第二本书的年份 51 Element yearTwo = document.createElement("year"); 52 yearTwo.setTextContent("2016"); 53 bookTwo.appendChild(yearTwo); 54 //生成第二本书的作者 55 Element authorTwo = document.createElement("author"); 56 authorTwo.setTextContent("jim"); 57 bookTwo.appendChild(authorTwo); 58 //将book节点添加到bookList节点中 59 bookList.appendChild(bookOne); 60 bookList.appendChild(bookTwo); 61 //将根节点添加到文档中 62 document.appendChild(bookList); 63 //DOM创建XML文档需要使用TransformerFactory创建实例对象 64 TransformerFactory tff = TransformerFactory.newInstance(); 65 //创建Transformer对象 66 Transformer tf = tff.newTransformer(); 67 //设置换行 68 tf.setOutputProperty(OutputKeys.INDENT, "yes"); 69 //调用Transformer对象的transform方法生成XML文档 70 tf.transform(new DOMSource(document), new StreamResult(new File("src\\xmls\\DOM.xml"))); 71 }catch (ParserConfigurationException e){ 72 e.printStackTrace(); 73 }catch (TransformerException e){ 74 e.printStackTrace(); 75 } 76 } 77 }
第二,使用SAX的方式生成。
1 package xmls; 2 import org.xml.sax.SAXException; 3 import org.xml.sax.helpers.AttributesImpl; 4 import javax.xml.transform.OutputKeys; 5 import javax.xml.transform.Result; 6 import javax.xml.transform.Transformer; 7 import javax.xml.transform.TransformerConfigurationException; 8 import javax.xml.transform.sax.SAXTransformerFactory; 9 import javax.xml.transform.sax.TransformerHandler; 10 import javax.xml.transform.stream.StreamResult; 11 import java.io.File; 12 import java.io.FileNotFoundException; 13 import java.io.FileOutputStream; 14 import java.io.IOException; 15 /** 16 * Created by lenovo on 2017-6-3. 17 */ 18 public class SAXDemo { 19 public static void main(String[] args){ 20 SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory 21 .newInstance(); 22 try { 23 TransformerHandler handler = tff.newTransformerHandler(); 24 Transformer tr = handler.getTransformer(); 25 tr.setOutputProperty(OutputKeys.INDENT, "yes"); 26 File f = new File("src/xmls/SAX.xml"); 27 if (!f.exists()) { 28 f.createNewFile(); 29 } 30 // 6.创建Result对象,并且使其与handler关联 31 Result result = new StreamResult(new FileOutputStream(f)); 32 handler.setResult(result); 33 // 7.利用handler对象进行xml文件内容的编写O 34 // 打开document 35 handler.startDocument(); 36 AttributesImpl attr = new AttributesImpl(); 37 handler.startElement("", "", "bookList", attr); 38 attr.clear(); 39 attr.addAttribute("", "", "id", "", "one"); 40 handler.startElement("", "", "book", attr); 41 attr.clear(); 42 handler.startElement("", "", "name", attr); 43 String title = "one book"; 44 handler.characters( title.toCharArray(), 0, title.length() ); 45 handler.endElement("", "", "name"); 46 handler.startElement("", "", "year", attr); 47 String year = "2017"; 48 handler.characters(year.toCharArray(), 0, year.length()); 49 handler.endElement("", "", "year"); 50 handler.startElement("", "", "author", attr); 51 String author = "tom"; 52 handler.characters(author.toCharArray(), 0, author.length()); 53 handler.endElement("", "", "author"); 54 handler.endElement("", "", "book"); 55 attr.clear(); 56 attr.addAttribute("", "", "id", "", "two"); 57 handler.startElement("", "", "book", attr); 58 attr.clear(); 59 handler.startElement("", "", "name", attr); 60 title = "two book"; 61 handler.characters( title.toCharArray(), 0, title.length() ); 62 handler.endElement("", "", "name"); 63 handler.startElement("", "", "year", attr); 64 year = "2016"; 65 handler.characters(year.toCharArray(), 0, year.length()); 66 handler.endElement("", "", "year"); 67 handler.startElement("", "", "author", attr); 68 author = "jim"; 69 handler.characters(author.toCharArray(), 0, author.length()); 70 handler.endElement("", "", "author"); 71 handler.endElement("", "", "book"); 72 handler.endElement("", "", "bookList"); 73 // 关闭document 74 handler.endDocument(); 75 }catch (TransformerConfigurationException e) { 76 e.printStackTrace(); 77 } catch (FileNotFoundException e) { 78 e.printStackTrace(); 79 } catch (IOException e) { 80 e.printStackTrace(); 81 } catch (SAXException e) { 82 e.printStackTrace(); 83 } 84 } 85 }
第三,使用JDOM的方式。
1 package xmls; 2 import org.jdom2.Document; 3 import org.jdom2.Element; 4 import org.jdom2.output.Format; 5 import org.jdom2.output.XMLOutputter; 6 import java.io.File; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 /** 11 * Created by lenovo on 2017-6-3. 12 */ 13 public class JDOMDemo { 14 public static void main(String[] args){ 15 Element bookList = new Element("bookList"); 16 Document document = new Document(bookList); 17 Element book = new Element("book"); 18 book.setAttribute("id", "one"); 19 Element name = new Element("name"); 20 name.setText("one book"); 21 book.addContent(name); 22 Element year = new Element("year"); 23 year.setText("2017"); 24 book.addContent(year); 25 Element author = new Element("author"); 26 author.setText("tom"); 27 book.addContent(author); 28 bookList.addContent(book); 29 book = new Element("book"); 30 book.setAttribute("id", "two"); 31 name = new Element("name"); 32 name.setText("two book"); 33 book.addContent(name); 34 year = new Element("year"); 35 year.setText("2016"); 36 book.addContent(year); 37 author = new Element("author"); 38 author.setText("jim"); 39 book.addContent(author); 40 bookList.addContent(book); 41 42 43 Format format = Format.getCompactFormat(); 44 format.setIndent(""); 45 XMLOutputter outputer = new XMLOutputter(format); 46 try { 47 //5.利用outputer将document对象转换成xml文档 48 outputer.output(document, new FileOutputStream(new File("src/xmls/JDOM.xml"))); 49 } catch (FileNotFoundException e) { 50 e.printStackTrace(); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 }
第四,使用DOM4J的方式生成。
1 package xmls; 2 import org.dom4j.Document; 3 import org.dom4j.DocumentHelper; 4 import org.dom4j.Element; 5 import org.dom4j.io.OutputFormat; 6 import org.dom4j.io.XMLWriter; 7 import java.io.File; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 /** 11 * Created by lenovo on 2017-6-3. 12 */ 13 public class DOM4JDemo { 14 public static void main(String[] args){ 15 Document document = DocumentHelper.createDocument(); 16 Element bookList = document.addElement("bookList"); 17 Element bookOne = bookList.addElement("book"); 18 bookOne.addAttribute("id", "one"); 19 Element nameOne = bookOne.addElement("name"); 20 nameOne.setText("one book"); 21 Element yearOne = bookOne.addElement("year"); 22 yearOne.setText("2017"); 23 Element authorOne = bookOne.addElement("author"); 24 authorOne.setText("tom"); 25 Element bookTwo = bookList.addElement("book"); 26 bookTwo.addAttribute("id", "two"); 27 Element nameTwo = bookTwo.addElement("name"); 28 nameTwo.setText("two book"); 29 Element yearTwo = bookTwo.addElement("year"); 30 yearTwo.setText("2016"); 31 Element authorTwo = bookTwo.addElement("author"); 32 authorTwo.setText("jim"); 33 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); 34 try{ 35 FileOutputStream out = new FileOutputStream(new File("src\\xmls\\DOM4J.xml")); 36 XMLWriter writer = new XMLWriter(out, outputFormat); 37 writer.write(document); 38 writer.close(); 39 }catch (IOException e){ 40 e.printStackTrace(); 41 } 42 } 43 }
以上是关于生成XML文档的主要内容,如果未能解决你的问题,请参考以下文章