2016年11月11日 星期五

XMLInputFactory

※XMLInputFactory


※xml.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <first fa="f1">
        <second1 sa="s1">aaa</second1>
        <second2>bbb</second2>
        <second3>ccc</second3>
    </first>
    
    <first fa="f2">
        <second1 sa="s2">ddd</second1>
        <second2>eee</second2>
        <second3>fff</second3>
    </first>
    
    <first fa="f3">
        <second1 sa="s3">ggg</second1>
        <second2>hhh</second2>
        <second3>iii</second3>
    </first>
</root>

※測試用的xml


※循序讀取

public void readXMLSequence() throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try (InputStream is = Test.class.getClassLoader().getResourceAsStream("xml.xml")) {
        XMLStreamReader reader = null;
        reader = factory.createXMLStreamReader(is);
        while (reader.hasNext()) {
            int type = reader.next();
            System.out.println("type=" + type);
    
            if (type == XMLStreamConstants.START_ELEMENT) {
                System.out.println("<" + reader.getName() + ">");
            } else if (type == XMLStreamConstants.CHARACTERS) {
                System.out.println("tag之間=" + reader.getText().trim());
            } else if (type == XMLStreamConstants.END_ELEMENT) {
                System.out.println("</" + reader.getName() + ">");
            }
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

※tag和tag之間也會讀取


※讀取屬性

public void readXMLAttribute() throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try (InputStream is = Test.class.getClassLoader().getResourceAsStream("xml.xml")) {
        XMLStreamReader reader = null;
        reader = factory.createXMLStreamReader(is);
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamConstants.START_ELEMENT) {
                String name = reader.getName().toString();
                if ("first".equals(name)) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        System.out.println("attribute name=" + reader.getAttributeName(i));
                        System.out.println("attribute value=" + reader.getAttributeValue(i));
                    }
                }
            }
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

※getResourceAsStream不熟的可看這篇


※讀取指定的元素

public void readXMLElement() throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try (InputStream is = Test.class.getClassLoader().getResourceAsStream("xml.xml")) {
        XMLStreamReader reader = null;
        reader = factory.createXMLStreamReader(is);
    
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamConstants.START_ELEMENT) {
                String name = reader.getName().toString();
                if ("second2".equals(name)) {
                    System.out.println("second2=" + reader.getElementText());
                }
    
                if ("second3".equals(name)) {
                    System.out.println("second3" + reader.getElementText() + System.getProperty("line.separator"));
                }
            }
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

※只抓元素是second2和second3


※讀取指定的元素2

public void readXMLElement2() throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try (InputStream is = Test.class.getClassLoader().getResourceAsStream("xml.xml")) {
        XMLEventReader reader = factory.createXMLEventReader(is);
    
        int count = 0;
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                String name = event.asStartElement().getName().toString();
                if ("second2".equals(name)) {
                    System.out.println("second2=" + reader.getElementText());
                }
                if ("second3".equals(name)) {
                    System.out.println("second3" + reader.getElementText());
                }
            }
            count++;
        }
        System.out.println(count);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

※使用XMLEvent


※使用過濾器

public void readXMLFilter() throws IOException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try (InputStream is = Test.class.getClassLoader().getResourceAsStream("xml.xml")) {
        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 (Arrays.asList("second2", "second3").contains(name)) {
                        return true;
                    }
                }
                return false;
            }
        });
    
        int count = 0;
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                String name = event.asStartElement().getName().toString();
                if ("second2".equals(name)) {
                    System.out.println("second2=" + reader.getElementText());
                }
                if ("second3".equals(name)) {
                    System.out.println("second3" + reader.getElementText());
                }
            }
            count++;
        }
        System.out.println(count);
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
}



沒有留言:

張貼留言