四种方式
- DOM方式
- SAX方式
- JDOM方式
- DOM4J方式
Maven依赖
<!--解析xml--> <!-- https://mvnrepository.com/artifact/org.jdom/jdom --> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>2.0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/dom4j/dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
1.DOM方式
package imooc.write.toxml import org.junit.Test import java.io.File import javax.xml.parsers.DocumentBuilderFactory import javax.xml.transform.OutputKeys import javax.xml.transform.TransformerFactory import javax.xml.transform.dom.DOMSource import javax.xml.transform.stream.StreamResult /** * @author futao * Created on 2017/11/6 - 15:23. * 使用DOM方式生成xml文件 */ class DOM { @Test fun xml() { val documentBuilderFactory = DocumentBuilderFactory.newInstance() val documentBuilder = documentBuilderFactory.newDocumentBuilder() //返回一个Document对象 val document = documentBuilder.newDocument() //取消xml文件头的standalone信息 document.xmlStandalone = true //创建了一个根节点 val element = document.createElement("books") val book = document.createElement("book") book.setAttribute("id", "1") book.setAttribute("id2", "2") //创建name子节点 val name = document.createElement("name") // name.nodeValue="Java实现生成xml文件" name.textContent = "Java实现生成xml文件" //添加到book子节点下 book.appendChild(name) //向根节点添加子节点 element.appendChild(book) //向DOM树中添加节点 document.appendChild(element) val transformerFactory = TransformerFactory.newInstance() val transformer = transformerFactory.newTransformer() //是否自动换行 transformer.setOutputProperty(OutputKeys.INDENT, "yes") transformer.transform(DOMSource(document), StreamResult(File("domBooks.xml"))) // val outputFormat = OutputFormat.createPrettyPrint() // val xmlWriter = XMLWriter(FileOutputStream(File("domBooks.xml"))) // xmlWriter.write(document) // xmlWriter.close() } }
2. SAX方式
package imooc.write.toxml import org.junit.Test import org.xml.sax.helpers.AttributesImpl import java.io.File import java.io.FileOutputStream import javax.xml.transform.OutputKeys import javax.xml.transform.sax.SAXTransformerFactory import javax.xml.transform.stream.StreamResult /** * @author futao * Created on 2017/11/6 - 15:44. * 使用SAX方式生成xml文件 */ class SAX { @Test fun xml() { // val saxParserFactory = SAXParserFactory.newInstance() // val saxParser = saxParserFactory.newSAXParser() val transformerFactory = SAXTransformerFactory.newInstance() as SAXTransformerFactory val transformerHandler = transformerFactory.newTransformerHandler() val transformer = transformerHandler.transformer //编码格式 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8") //是否换行 transformer.setOutputProperty(OutputKeys.INDENT, "yes") val result = StreamResult(FileOutputStream(File("saxBooks.xml"))) transformerHandler.setResult(result) //打开文档 transformerHandler.startDocument() val attributesImpl = AttributesImpl() //books根节点开始 transformerHandler.startElement("", "", "books", attributesImpl) attributesImpl.addAttribute("", "", "id", "", "1") //给子节点的属性赋值 //book子节点开始 transformerHandler.startElement("", "", "book", attributesImpl) //清除之前的属性值 attributesImpl.clear() //book节点的子节点name节点开始 transformerHandler.startElement("", "", "name", attributesImpl) //向name节点中添加值 transformerHandler.characters("从入门到精通".toCharArray(), 0, "从入门到精通".toCharArray().size) //book节点的子节点name节点结束 transformerHandler.endElement("", "", "name") //book子节点结束 transformerHandler.endElement("", "", "book") //books根节点结束 transformerHandler.endElement("", "", "books") //关闭文档 transformerHandler.endDocument() } }
3. JDOM方式
package imooc.write.toxml import org.jdom2.CDATA import org.jdom2.Document import org.jdom2.Element import org.jdom2.output.Format import org.jdom2.output.XMLOutputter import org.junit.Test import java.io.File import java.io.FileOutputStream /** * @author futao * Created on 2017/11/6 - 18:59. * JDOM方式生成xml */ class JDOM { @Test fun xml() { //生成rss根节点 val rss = Element("rss") //给rss节点添加属性值 rss.setAttribute("id", "1") //生成channel节点 val channel = Element("channel") //给rss节点添加channel子节点 rss.addContent(channel) //生成title节点 val title = Element("title") //设置title的内容 title.text = " 我是 title " //把title节点添加到channel的子节点 channel.addContent(title) val link1 = Element("link1") channel.addContent(link1) val content = CDATA("link") content.text = ">>><<<<www.baidu.com" link1.addContent(content) val link2 = Element("link2") link2.text = ">>><<<www.baidu.com" channel.addContent(link2) //生成一个Document对象 val document = Document(rss) //格式化xml,换行和缩进 val prettyFormat = Format.getPrettyFormat() // prettyFormat.escapeStrategy = EscapeStrategy(function = { false }) //取消trim prettyFormat.textMode = Format.TextMode.TRIM_FULL_WHITE //创建XMLOutputter对象 val xmlOutputter = XMLOutputter(prettyFormat) //通过XMLOutputter将Document转换成xml文档 xmlOutputter.output(document, FileOutputStream(File("jdomBooks.xml"))) } }
4. DOM4J方式
package imooc.write.toxml import org.dom4j.DocumentHelper import org.dom4j.io.OutputFormat import org.dom4j.io.XMLWriter import org.junit.Test import java.io.File import java.io.FileOutputStream /** * @author futao * Created on 2017/11/6 - 17:10. * DOM4J方法生成xml * 个人觉得比较方便 */ class DOM4J { @Test fun xml() { //创建xml对象 val document = DocumentHelper.createDocument() //添加rss节点 val rss = document.addElement("rss") //rss节点的version属性 rss.addAttribute("version", "2.0") //生成子节点和子节点信息 val channel = rss.addElement("channel") val title = channel.addElement("title") title.addText("国内新闻") val link = channel.addElement("link") link.addText(">><<http://www.baidu.com") val link2 = channel.addElement("link2") // link2.addCDATA(">><<http://www.baidu.com") //设置生成xml的格式,换行,缩进 val prettyPrint = OutputFormat.createPrettyPrint() prettyPrint.encoding = "UTF-8" //生成xml文件 val xmlWriter = XMLWriter(FileOutputStream(File("dom4jBooks.xml")), prettyPrint) //不转义 xmlWriter.isEscapeText = false xmlWriter.write(document) xmlWriter.close() } }