半步多 玄玉的博客

使用StAX读写XML文件

2013-03-19
玄玉
XML
 

下面本文示例中,读取的XML内容

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book category="COOKING" aaaaaa="111111">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="WEB">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
    </book>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>

基于Filter的方式解析XML

package com.jadyer.demo.xml;
import org.apache.commons.io.IOUtils;
import javax.xml.stream.EventFilter;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * StAX(Streaming API for XML)的方式解析XML
 * Created by 玄玉<https://jadyer.cn/> on 2013/03/19 15:03.
 */
public class StaxXMLDemo {
    public static void main(String[] args) throws Exception {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<bookstore>\n" +
                "    <book category=\"COOKING\" aaaaaa=\"111111\">\n" +
                "        <title lang=\"en\">Everyday Italian</title>\n" +
                "        <author>Giada De Laurentiis</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>30.00</price>\n" +
                "    </book>\n" +
                "    <book category=\"CHILDREN\">\n" +
                "        <title lang=\"en\">Harry Potter</title>\n" +
                "        <author>J K. Rowling</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>29.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">XQuery Kick Start</title>\n" +
                "        <author>James McGovern</author>\n" +
                "        <author>Per Bothner</author>\n" +
                "        <author>Kurt Cagle</author>\n" +
                "        <author>James Linn</author>\n" +
                "        <author>Vaidyanathan Nagarajan</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>49.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">Learning XML</title>\n" +
                "        <author>Erik T. Ray</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>39.95</price>\n" +
                "    </book>\n" +
                "</bookstore>";
        new StaxXMLDemo().filterParseXML(xmlStr);
    }

    /**
     * 基于Filter的方式解析XML
     * 可以有效的过滤掉不用进行操作的节点,效率会高一些
     */
    private void filterParseXML(String xmlStr) throws Exception {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        //InputStream is = StaxXMLDemo.class.getClassLoader().getResourceAsStream("books.xml")
        InputStream is = IOUtils.toInputStream(xmlStr, StandardCharsets.UTF_8);
        XMLEventReader reader = factory.createFilteredReader(factory.createXMLEventReader(is),
                new EventFilter() {
                    @Override
                    public boolean accept(XMLEvent event) {
                        if(event.isStartElement()) {
                            String name = event.asStartElement().getName().toString();
                            if(name.equals("title") || name.equals("price")){
                                //返回true表示会显示,false表示不显示
                                return true;
                            }
                        }
                        return false;
                    }
                });
        //统计遍历的次数
        int iteratorNum = 0;
        while(reader.hasNext()){
            XMLEvent event = reader.nextEvent();
            if(event.isStartElement()){
                String name = event.asStartElement().getName().toString();
                if(name.equals("title")){
                    System.out.print("书籍[" + reader.getElementText() + "]");
                }
                if(name.equals("price")){
                    System.out.println("的价格为[" + reader.getElementText() + "]");
                }
            }
            iteratorNum++;
        }
        is.close();
        System.out.println("本次操作遍历XML的次数合计为[" + iteratorNum + "]次");
    }
}

基于Filter的方式解析XML,控制台输出如下

书籍[Everyday Italian]的价格为[30.00]
书籍[Harry Potter]的价格为[29.99]
书籍[XQuery Kick Start]的价格为[49.99]
书籍[Learning XML]的价格为[39.95]
本次操作遍历XML的次数合计为[8]次

基于XPath的方式解析XML

package com.jadyer.demo.xml;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * StAX(Streaming API for XML)的方式解析XML
 * Created by 玄玉<https://jadyer.cn/> on 2013/03/19 15:03.
 */
public class StaxXMLDemo {
    public static void main(String[] args) throws Exception {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<bookstore>\n" +
                "    <book category=\"COOKING\" aaaaaa=\"111111\">\n" +
                "        <title lang=\"en\">Everyday Italian</title>\n" +
                "        <author>Giada De Laurentiis</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>30.00</price>\n" +
                "    </book>\n" +
                "    <book category=\"CHILDREN\">\n" +
                "        <title lang=\"en\">Harry Potter</title>\n" +
                "        <author>J K. Rowling</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>29.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">XQuery Kick Start</title>\n" +
                "        <author>James McGovern</author>\n" +
                "        <author>Per Bothner</author>\n" +
                "        <author>Kurt Cagle</author>\n" +
                "        <author>James Linn</author>\n" +
                "        <author>Vaidyanathan Nagarajan</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>49.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">Learning XML</title>\n" +
                "        <author>Erik T. Ray</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>39.95</price>\n" +
                "    </book>\n" +
                "</bookstore>";
        new StaxXMLDemo().xpathParseXML(xmlStr);
    }

    /**
     * 基于XPath的方式解析XML
     */
    private void xpathParseXML(String xmlStr) throws Exception {
        //InputStream is = StaxXMLDemo.class.getClassLoader().getResourceAsStream("books.xml")
        InputStream is = IOUtils.toInputStream(xmlStr, StandardCharsets.UTF_8);
        //创建文档处理对象
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        //通过DocumentBuilder创建文档对象
        Document doc = documentBuilder.parse(is);
        //创建XPath
        XPath xpath = XPathFactory.newInstance().newXPath();
        //第一个参数就是XPath表达式,第二参数就是文档
        //这里是查找所有属性为category="WEB"的<book>节点
        NodeList nodeList = (NodeList)xpath.evaluate("//book[@category='WEB']", doc, XPathConstants.NODESET);
        for(int i=0; i<nodeList.getLength(); i++) {
            Element e = (Element)nodeList.item(i);
            //由于<book>节点下只会有一个<title>节点,所有这里是item(0)
            System.out.println(e.getElementsByTagName("title").item(0).getTextContent());
            System.out.println(e.getElementsByTagName("price").item(0).getTextContent());
        }
        is.close();
    }
}

基于XPath的方式解析XML,控制台输出如下

XQuery Kick Start
49.99
Learning XML
39.95

基于光标的方式解析XML

package com.jadyer.demo.xml;
import org.apache.commons.io.IOUtils;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * StAX(Streaming API for XML)的方式解析XML
 * Created by 玄玉<https://jadyer.cn/> on 2013/03/19 15:03.
 */
public class StaxXMLDemo {
    public static void main(String[] args) throws Exception {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<bookstore>\n" +
                "    <book category=\"COOKING\" aaaaaa=\"111111\">\n" +
                "        <title lang=\"en\">Everyday Italian</title>\n" +
                "        <author>Giada De Laurentiis</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>30.00</price>\n" +
                "    </book>\n" +
                "    <book category=\"CHILDREN\">\n" +
                "        <title lang=\"en\">Harry Potter</title>\n" +
                "        <author>J K. Rowling</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>29.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">XQuery Kick Start</title>\n" +
                "        <author>James McGovern</author>\n" +
                "        <author>Per Bothner</author>\n" +
                "        <author>Kurt Cagle</author>\n" +
                "        <author>James Linn</author>\n" +
                "        <author>Vaidyanathan Nagarajan</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>49.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">Learning XML</title>\n" +
                "        <author>Erik T. Ray</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>39.95</price>\n" +
                "    </book>\n" +
                "</bookstore>";
        new StaxXMLDemo().cursorParseXML(xmlStr);
    }

    /**
     * 基于光标的方式解析XML
     */
    private void cursorParseXML(String xmlStr) throws Exception {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        //InputStream is = StaxXMLDemo.class.getClassLoader().getResourceAsStream("books.xml")
        InputStream is = IOUtils.toInputStream(xmlStr, StandardCharsets.UTF_8);
        XMLStreamReader reader = factory.createXMLStreamReader(is);
        while(reader.hasNext()){
            int type = reader.next();
            //处理不同的节点类型
            //处理开始节点,如<bookstore>,<book>
            if(type == XMLStreamConstants.START_ELEMENT){
                String name = reader.getName().toString();
                System.out.println("<" + name + ">");
                //打印<book category="COOKING" aaaaaa="111111">节点中的第二个属性值aaaaaa
                if(name.equals("book")){
                    System.out.println("节点属性值为" + reader.getAttributeName(1) + "=" + reader.getAttributeValue(1));
                }
                if(name.equals("title")){
                    System.out.println("书籍[" + reader.getElementText() + "]");
                }
                if(name.equals("price")){
                    System.out.println("的价格为[" + reader.getElementText() + "]");
                }
                //打印文本节点的内容
                //注意各xml标签中的空白部分也属于文本节点,并且文本节点是没有name的,所以不能getName
            }else if(type == XMLStreamConstants.CHARACTERS){
                System.out.println("文本节点值=[" + reader.getText().trim() + "]");
                //处理结束节点,如</book>,</title>
            }else if(type == XMLStreamConstants.END_ELEMENT){
                System.out.println("</" + reader.getName() + ">");
            }
        }
        //处理完XML记得关闭流,但不能提前关闭,因为Stax是读一点流就解析一点的
        is.close();
    }
}

基于光标的方式解析XML,控制台输出如下

<bookstore>
文本节点值=[]
<book>
节点属性值为aaaaaa=111111
文本节点值=[]
<title>
书籍[Everyday Italian]
文本节点值=[]
<author>
文本节点值=[Giada De Laurentiis]
</author>
文本节点值=[]
<year>
文本节点值=[2005]
</year>
文本节点值=[]
<price>
的价格为[30.00]
文本节点值=[]
</book>
文本节点值=[]
<book>
节点属性值为null=null
文本节点值=[]
<title>
书籍[Harry Potter]
文本节点值=[]
<author>
文本节点值=[J K. Rowling]
</author>
文本节点值=[]
<year>
文本节点值=[2005]
</year>
文本节点值=[]
<price>
的价格为[29.99]
文本节点值=[]
</book>
文本节点值=[]
<book>
节点属性值为null=null
文本节点值=[]
<title>
书籍[XQuery Kick Start]
文本节点值=[]
<author>
文本节点值=[James McGovern]
</author>
文本节点值=[]
<author>
文本节点值=[Per Bothner]
</author>
文本节点值=[]
<author>
文本节点值=[Kurt Cagle]
</author>
文本节点值=[]
<author>
文本节点值=[James Linn]
</author>
文本节点值=[]
<author>
文本节点值=[Vaidyanathan Nagarajan]
</author>
文本节点值=[]
<year>
文本节点值=[2003]
</year>
文本节点值=[]
<price>
的价格为[49.99]
文本节点值=[]
</book>
文本节点值=[]
<book>
节点属性值为null=null
文本节点值=[]
<title>
书籍[Learning XML]
文本节点值=[]
<author>
文本节点值=[Erik T. Ray]
</author>
文本节点值=[]
<year>
文本节点值=[2003]
</year>
文本节点值=[]
<price>
的价格为[39.95]
文本节点值=[]
</book>
文本节点值=[]
</bookstore>

基于迭代模型的方式解析XML

package com.jadyer.demo.xml;
import org.apache.commons.io.IOUtils;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * StAX(Streaming API for XML)的方式解析XML
 * Created by 玄玉<https://jadyer.cn/> on 2013/03/19 15:03.
 */
public class StaxXMLDemo {
    public static void main(String[] args) throws Exception {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<bookstore>\n" +
                "    <book category=\"COOKING\" aaaaaa=\"111111\">\n" +
                "        <title lang=\"en\">Everyday Italian</title>\n" +
                "        <author>Giada De Laurentiis</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>30.00</price>\n" +
                "    </book>\n" +
                "    <book category=\"CHILDREN\">\n" +
                "        <title lang=\"en\">Harry Potter</title>\n" +
                "        <author>J K. Rowling</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>29.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">XQuery Kick Start</title>\n" +
                "        <author>James McGovern</author>\n" +
                "        <author>Per Bothner</author>\n" +
                "        <author>Kurt Cagle</author>\n" +
                "        <author>James Linn</author>\n" +
                "        <author>Vaidyanathan Nagarajan</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>49.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">Learning XML</title>\n" +
                "        <author>Erik T. Ray</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>39.95</price>\n" +
                "    </book>\n" +
                "</bookstore>";
        new StaxXMLDemo().iteratorParseXML(xmlStr);
    }

    /**
     * 基于迭代模型的方式解析XML
     */
    private void iteratorParseXML(String xmlStr) throws Exception {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        //InputStream is = StaxXMLDemo.class.getClassLoader().getResourceAsStream("books.xml")
        InputStream is = IOUtils.toInputStream(xmlStr, StandardCharsets.UTF_8);
        XMLEventReader reader = factory.createXMLEventReader(is);
        //统计遍历的次数
        int iteratorNum = 0;
        while(reader.hasNext()){
            XMLEvent event = reader.nextEvent();
            //通过XMLEvent判断节点类型
            if(event.isStartElement()){
                //通过event.asxxx转换节点
                String name = event.asStartElement().getName().toString();
                if(name.equals("title")){
                    System.out.print("书籍[" + reader.getElementText() + "]");
                }
                if(name.equals("price")){
                    System.out.println("的价格为[" + reader.getElementText() + "]");
                }
            }
            iteratorNum++;
        }
        is.close();
        System.out.println("本次操作遍历XML的次数合计为[" + iteratorNum + "]次");
    }
}

基于迭代模型的方式解析XML,控制台输出如下

书籍[Everyday Italian]的价格为[30.00]
书籍[Harry Potter]的价格为[29.99]
书籍[XQuery Kick Start]的价格为[49.99]
书籍[Learning XML]的价格为[39.95]
本次操作遍历XML的次数合计为[85]次

修改XML

package com.jadyer.demo.xml;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * StAX(Streaming API for XML)的方式解析XML
 * Created by 玄玉<https://jadyer.cn/> on 2013/03/19 15:03.
 */
public class StaxXMLDemo {
    public static void main(String[] args) throws Exception {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<bookstore>\n" +
                "    <book category=\"COOKING\" aaaaaa=\"111111\">\n" +
                "        <title lang=\"en\">Everyday Italian</title>\n" +
                "        <author>Giada De Laurentiis</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>30.00</price>\n" +
                "    </book>\n" +
                "    <book category=\"CHILDREN\">\n" +
                "        <title lang=\"en\">Harry Potter</title>\n" +
                "        <author>J K. Rowling</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>29.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">XQuery Kick Start</title>\n" +
                "        <author>James McGovern</author>\n" +
                "        <author>Per Bothner</author>\n" +
                "        <author>Kurt Cagle</author>\n" +
                "        <author>James Linn</author>\n" +
                "        <author>Vaidyanathan Nagarajan</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>49.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">Learning XML</title>\n" +
                "        <author>Erik T. Ray</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>39.95</price>\n" +
                "    </book>\n" +
                "</bookstore>";
        new StaxXMLDemo().transformerModifyXML(xmlStr);
    }

    /**
     * 使用Transformer修改XML
     */
    private void transformerModifyXML(String xmlStr) throws Exception, TransformerFactoryConfigurationError {
        //InputStream is = StaxXMLDemo.class.getClassLoader().getResourceAsStream("books.xml")
        InputStream is = IOUtils.toInputStream(xmlStr, StandardCharsets.UTF_8);
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = documentBuilder.parse(is);
        XPath xpath = XPathFactory.newInstance().newXPath();
        //查找内容含title="Learning XML"的<book>节点
        NodeList list = (NodeList)xpath.evaluate("//book[title='Learning XML']", doc, XPathConstants.NODESET);
        //获取<price>节点,这里只有一个符合条件的<book>节点,所以写成了list.item(0)
        Element e = (Element)(((Element)list.item(0)).getElementsByTagName("price").item(0));
        e.setTextContent("333.9");
        Transformer tran = TransformerFactory.newInstance().newTransformer();
        tran.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        //让<?xml version=...?>与<bookestore>节点之间换行(默认不换行)
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        //修改节点
        tran.transform(new DOMSource(doc), new StreamResult(System.out));
        is.close();
    }
}

使用Transformer修改XML,控制台输出如下

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookstore>
    <book aaaaaa="111111" category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="WEB">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
    </book>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>333.9</price>
    </book>
</bookstore>

创建XML

package com.jadyer.demo.xml;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

/**
 * StAX(Streaming API for XML)的方式解析XML
 * Created by 玄玉<https://jadyer.cn/> on 2013/03/19 15:03.
 */
public class StaxXMLDemo {
    public static void main(String[] args) throws Exception {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<bookstore>\n" +
                "    <book category=\"COOKING\" aaaaaa=\"111111\">\n" +
                "        <title lang=\"en\">Everyday Italian</title>\n" +
                "        <author>Giada De Laurentiis</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>30.00</price>\n" +
                "    </book>\n" +
                "    <book category=\"CHILDREN\">\n" +
                "        <title lang=\"en\">Harry Potter</title>\n" +
                "        <author>J K. Rowling</author>\n" +
                "        <year>2005</year>\n" +
                "        <price>29.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">XQuery Kick Start</title>\n" +
                "        <author>James McGovern</author>\n" +
                "        <author>Per Bothner</author>\n" +
                "        <author>Kurt Cagle</author>\n" +
                "        <author>James Linn</author>\n" +
                "        <author>Vaidyanathan Nagarajan</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>49.99</price>\n" +
                "    </book>\n" +
                "    <book category=\"WEB\">\n" +
                "        <title lang=\"en\">Learning XML</title>\n" +
                "        <author>Erik T. Ray</author>\n" +
                "        <year>2003</year>\n" +
                "        <price>39.95</price>\n" +
                "    </book>\n" +
                "</bookstore>";
        new StaxXMLDemo().xmlStreamWriterXML(xmlStr);
    }

    /**
     * 使用XMLStreamWriter创建XML
     */
    private void xmlStreamWriterXML(String xmlStr) throws XMLStreamException, FactoryConfigurationError {
        XMLStreamWriter xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
        //先写<?xml version="1.0" encoding="UTF-8"?>
        xsw.writeStartDocument("UTF-8", "1.0");
        xsw.writeEndDocument();
        String namespaceURI = "https://jadyer.cn/";
        //再写<pre:uesr>
        xsw.writeStartElement("pre", "uesr", namespaceURI);
        //再写<pre:id>
        xsw.writeStartElement(namespaceURI, "id");
        //再写1
        xsw.writeCharacters("1");
        //再写</pre:id>
        xsw.writeEndElement();
        //再写</pre:uesr>
        xsw.writeEndElement();
        xsw.flush();
        xsw.close();
    }
}

使用XMLStreamWriter创建XML,控制台输出如下

<?xml version="1.0" encoding="UTF-8"?><pre:uesr><pre:id>1</pre:id></pre:uesr>

Comments

Content