四种方式
- DOM方式
- SAX方式
- JDOM方式
- DOM4J方式
Maven依赖
org.jdom
jdom
2.0.2
dom4j
dom4j
1.6.1
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 = ">>><<<
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(">><><