Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Přístup k XML souborům

Zde je ukázka jednoduchého přístupu k XML souborům bez DTD pomocí javax.xml.stream.XMLStreamReader. Jedná se jen o základní demosntraci použití této třídy k read-only přístupu k datům a atributům v souboru.

Demonstrační XML soubor je zde.

package cz.cvut.fel.pjv;
 
import java.io.FileInputStream;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
 
public class SimpleXMLReader {
 
    public static void main(String[] args) throws Exception {
        InputStream in = new FileInputStream("c:\\tmp\\input.xml");
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader parser = factory.createXMLStreamReader(in);
        while (parser.hasNext()) {
            System.out.println("Zjistuji novy event!!!!!");
            int event = parser.next();
            System.out.println("Event type:"+parser.getEventType());
            if (event == XMLStreamConstants.START_ELEMENT) {
                System.out.println(">Event type START_ELEMENT");
                System.out.println(">TAG:"+parser.getLocalName());
                System.out.println("->Atribute count:"+parser.getAttributeCount());
                if (parser.getAttributeCount() > 0) {
                    for (int i = 0; i < parser.getAttributeCount(); i++) {
                        System.out.println("-->Atribute name:" + parser.getAttributeName(i));
                        System.out.println("-->Atribute type:" + parser.getAttributeType(i));
                        System.out.println("-->Atribute value:" + parser.getAttributeValue(i));
                    }
                }               
            }
 
            if (event == XMLStreamConstants.CHARACTERS){
                System.out.println(">Event type CHARACTERS"); 
                System.out.println("->Text: >"+parser.getText()+"<");
            }
            if (event == XMLStreamConstants.END_ELEMENT){
                System.out.println(">Event type END_ELEMENT");
                System.out.println(">TAG:"+parser.getLocalName());
            }
 
        }
    }
}

courses/b0b36pjv/tutorials/08/xml.txt · Last modified: 2018/02/06 08:43 (external edit)