Java HTML/XML - 如何使用SAX解析器從大XML文件獲取嵌入/嵌套XML
我們想知道如何使用SAX解析器從大XML文件獲取嵌入/嵌套XML。
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xmlSource = new StreamSource("src/forum19559825/input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xmlSource);
positionXMLStreamReaderAtAnyElement(xsr);
processAnyElement(xsr);
}
private static void positionXMLStreamReaderAtAnyElement(XMLStreamReader xsr)
throws Exception {
while (xsr.hasNext()) {
if (xsr.getEventType() == XMLStreamReader.START_ELEMENT
&& "ANY-ELEMENT".equals(xsr.getLocalName())) {
break;
}
xsr.next();
}
}
private static void processAnyElement(
XMLStreamReader xmlStreamReaderAtAnyElement) {
System.out.println("FOUND IT");
}
}