了解 XMLStreamReader 和 START_ELEMENT
Understanding XMLStreamReader and START_ELEMENT
考虑以下 XML 文件:
% cat test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
</root>
为什么我在使用 XMLStreamReader. Code is (lifted from 时没有收到 START_ELEMENT
事件:
% cat Demo.java
import java.io.FileReader;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
System.out.println(sr.getClass());
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamReader.START_DOCUMENT) {
System.out.println("Start Document" );
} else if (eventType == XMLStreamReader.END_DOCUMENT) {
System.out.println("End Document" );
} else if (eventType == XMLStreamReader.END_ELEMENT) {
System.out.println("End Element: " + sr.getLocalName());
} else if (eventType == XMLStreamReader.START_ELEMENT) {
System.out.println("Start Element: " + sr.getLocalName());
}
}
}
}
我这边的输出:
% javac Demo.java
% java Demo test.xml
class com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl
Start Element: root
End Element: root
End Document
参考:
% java --version
openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.14+9-post-Debian-1deb11u1, mixed mode, sharing)
原来文档有描述:
An XMLStreamReader instance is created with an initial event type
START_DOCUMENT.
所以代码应该改为:
% cat Demo.java
import java.io.FileReader;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
System.out.println(sr.getClass());
boolean hasNext;
do {
int eventType = sr.getEventType();
if (eventType == XMLStreamReader.START_DOCUMENT) {
System.out.println("Start Document" );
} else if (eventType == XMLStreamReader.END_DOCUMENT) {
System.out.println("End Document" );
} else if (eventType == XMLStreamReader.END_ELEMENT) {
System.out.println("End Element: " + sr.getLocalName());
} else if (eventType == XMLStreamReader.START_ELEMENT) {
System.out.println("Start Element: " + sr.getLocalName());
}
hasNext = sr.hasNext();
if(hasNext) sr.next();
} while( hasNext );
}
}
循环使用XMLEventReader更容易写:
% cat Demo2.java
import java.io.FileReader;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
public class Demo2 {
public static void main(String[] args) throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader er = factory.createXMLEventReader(new FileReader("test.xml"));
System.out.println(er.getClass());
while(er.hasNext()) {
XMLEvent xmlEvent = er.nextEvent();
int eventType = xmlEvent.getEventType();
if (eventType == XMLStreamConstants.START_DOCUMENT) {
System.out.println("Start Document" );
} else if (eventType == XMLStreamConstants.END_DOCUMENT) {
System.out.println("End Document" );
} else if (eventType == XMLStreamConstants.END_ELEMENT) {
System.out.println("End Element: " + xmlEvent.asEndElement().getName());
} else if (eventType == XMLStreamConstants.START_ELEMENT) {
System.out.println("Start Element: " + xmlEvent.asStartElement().getName());
}
}
}
}
考虑以下 XML 文件:
% cat test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
</root>
为什么我在使用 XMLStreamReader. Code is (lifted from 时没有收到 START_ELEMENT
事件:
% cat Demo.java
import java.io.FileReader;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
System.out.println(sr.getClass());
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamReader.START_DOCUMENT) {
System.out.println("Start Document" );
} else if (eventType == XMLStreamReader.END_DOCUMENT) {
System.out.println("End Document" );
} else if (eventType == XMLStreamReader.END_ELEMENT) {
System.out.println("End Element: " + sr.getLocalName());
} else if (eventType == XMLStreamReader.START_ELEMENT) {
System.out.println("Start Element: " + sr.getLocalName());
}
}
}
}
我这边的输出:
% javac Demo.java
% java Demo test.xml
class com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl
Start Element: root
End Element: root
End Document
参考:
% java --version
openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.14+9-post-Debian-1deb11u1, mixed mode, sharing)
原来文档有描述:
An XMLStreamReader instance is created with an initial event type START_DOCUMENT.
所以代码应该改为:
% cat Demo.java
import java.io.FileReader;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
System.out.println(sr.getClass());
boolean hasNext;
do {
int eventType = sr.getEventType();
if (eventType == XMLStreamReader.START_DOCUMENT) {
System.out.println("Start Document" );
} else if (eventType == XMLStreamReader.END_DOCUMENT) {
System.out.println("End Document" );
} else if (eventType == XMLStreamReader.END_ELEMENT) {
System.out.println("End Element: " + sr.getLocalName());
} else if (eventType == XMLStreamReader.START_ELEMENT) {
System.out.println("Start Element: " + sr.getLocalName());
}
hasNext = sr.hasNext();
if(hasNext) sr.next();
} while( hasNext );
}
}
循环使用XMLEventReader更容易写:
% cat Demo2.java
import java.io.FileReader;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
public class Demo2 {
public static void main(String[] args) throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader er = factory.createXMLEventReader(new FileReader("test.xml"));
System.out.println(er.getClass());
while(er.hasNext()) {
XMLEvent xmlEvent = er.nextEvent();
int eventType = xmlEvent.getEventType();
if (eventType == XMLStreamConstants.START_DOCUMENT) {
System.out.println("Start Document" );
} else if (eventType == XMLStreamConstants.END_DOCUMENT) {
System.out.println("End Document" );
} else if (eventType == XMLStreamConstants.END_ELEMENT) {
System.out.println("End Element: " + xmlEvent.asEndElement().getName());
} else if (eventType == XMLStreamConstants.START_ELEMENT) {
System.out.println("Start Element: " + xmlEvent.asStartElement().getName());
}
}
}
}