XMLStreamWriter Java StAX
时间:2020-02-23 14:35:39 来源:igfitidea点击:
在上一篇文章中,我们学习了如何使用Java StAX Iterator API编写XML,这里我们将使用StAX光标API编写XML文件。
XMLStreamWriter
Java StAX Cursor API在创建XML和输出XML方面非常直接。
我们需要创建XMLStreamWriter对象并将数据写入其中。
有用的方法是writeStartDocument(),writeStartElement(),writeCharacters(),writeEndElement()和writeEndDocument()。
XMLStreamWriter示例
其中我们将创建一个HashMap,然后将其写入XML文件。
package com.theitroad.xml.stax; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; public class StaxXMLStreamWriter { public static void main(String[] args) { String fileName = "/Users/hyman/tmp/employee.xml"; String rootElement = "Employee"; StaxXMLStreamWriter xmlStreamWriter = new StaxXMLStreamWriter(); Map<String,String> elementsMap = new HashMap<String, String>(); elementsMap.put("id", "1"); elementsMap.put("name", "hyman"); elementsMap.put("age", "29"); elementsMap.put("role", "Java Developer"); elementsMap.put("gender", "Male"); xmlStreamWriter.writeXML(fileName, rootElement, elementsMap); } private void writeXML(String fileName, String rootElement, Map<String, String> elementsMap) { XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); try{ XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(new FileOutputStream(fileName), "UTF-8"); //start writing xml file xmlStreamWriter.writeStartDocument("UTF-8", "1.0"); xmlStreamWriter.writeCharacters("\n"); xmlStreamWriter.writeStartElement(rootElement); //write id as attribute xmlStreamWriter.writeAttribute("id", elementsMap.get("id")); //write other elements xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeStartElement("name"); xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("name")); xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeStartElement("age"); xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("age")); xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeStartElement("gender"); xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("gender")); xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeStartElement("role"); xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("role")); xmlStreamWriter.writeCharacters("\n\t"); xmlStreamWriter.writeEndElement(); //write end tag of Employee element xmlStreamWriter.writeCharacters("\n"); xmlStreamWriter.writeEndElement(); //write end document xmlStreamWriter.writeEndDocument(); //flush data to file and close writer xmlStreamWriter.flush(); xmlStreamWriter.close(); }catch(XMLStreamException | FileNotFoundException e){ e.printStackTrace(); } } }
当我们执行上面的XMLStreamWriter示例程序时,我们得到以下XML文件。
<?xml version="1.0" encoding="UTF-8"?> <Employee id="1"> <name> hyman </name> <age> 29 </age> <gender> Male </gender> <role> Java Developer </role> </Employee>
请注意,为漂亮的打印XML使用了writeCharacters(),否则它将采用紧凑格式。