Java 操作XML (Stax JDK6引入)

半世苍凉 提交于 2020-04-08 12:48:41
package hello;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.jar.Attributes;

public class XML_STAXReader {
    public static void main(String[] args) {
//        readByStream();
//        readByEvent();
    }

    /**
     * 流模式
     */
    static void readByStream() {
        String xmlFile = "users.xml";
        XMLInputFactory factory = XMLInputFactory.newFactory();
        XMLStreamReader streamReader = null;
        try {
            streamReader = factory.createXMLStreamReader(new FileReader(xmlFile));
        } catch (FileNotFoundException | XMLStreamException e) {
            e.printStackTrace();
        }

        // 基于指针遍历
        try {
            assert streamReader != null;
            while (streamReader.hasNext()) {
                int event = streamReader.next();
                // 如果是元素的开始
                if (event == XMLStreamConstants.START_ELEMENT) {
                    // 列出所有书籍名称
                    if ("name".equalsIgnoreCase(streamReader.getLocalName())) {
                        System.out.println("name: " + streamReader.getElementText());
                    }
                }
            }
            streamReader.close();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }

    }

    /**
     * 事件模式
     */
    static void readByEvent() {
        String xmlFile = "users.xml";
        XMLInputFactory factory = XMLInputFactory.newInstance();
        boolean nameFlag = false;
        try {
            // 创建基于迭代器的事件读取器对象
            XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(xmlFile));
            // 遍历 Event 迭代器
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                // 如果事件对象是元素的开始
                if (event.isStartElement()) {
                    // 转换成开始元素事件对象
                    StartElement start = event.asStartElement();
                    // 打印元素标签的本地名称
                    String name = start.getName().getLocalPart();
                    if (name.equals("name")) {
                        nameFlag = true;
                        System.out.println("name:");
                    }
                    // 取得所有属性
                    Iterator attrs = start.getAttributes();
                    while (attrs.hasNext()) {
                        // 打印所有属性信息
                        Attribute attr = (Attribute) attrs.next();
                        System.out.println(attr.getName().getLocalPart() + "=" + attr.getValue());
                    }
                }
                // 如果是正文
                if (event.isCharacters()) {
                    String s = event.asCharacters().getData();
                    if (s != null && s.trim().length() > 0 && nameFlag) {
                        System.out.println(s.trim());
                    }
                }

                // 如果是事件对象的元素的结束
                if (event.isEndElement()) {
                    EndElement end = event.asEndElement();
                    String name = end.getName().getLocalPart();
                    if (name.equals("name")) {
                        nameFlag = false;
                    }
                }
            }
            eventReader.close();
        } catch (FileNotFoundException | XMLStreamException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<users>
    <user id="001">
        <name>Mary</name>
        <age>3</age>
        <sex>Female</sex>
    </user>
    <user id="002">
        <name>Mike</name>
        <age>18</age>
        <sex>Male</sex>
    </user>
    <user id="003">
        <name>ZhaZhaHui</name>
        <age>38</age>
        <sex>Male</sex>
    </user>
</users>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!