Parse XML document file with XML Stream Reader in Java
Description
The following code shows how to parse XML document file with XML Stream Reader.
Example
//from www .j a v a 2 s . c o m
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 Main{
public static void main(String[] args) throws Exception{
InputStream in = new FileInputStream("a.xml");
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader parser = factory.createXMLStreamReader(in);
while (parser.hasNext()){
int event = parser.next();
if (event == XMLStreamConstants.START_ELEMENT){
if (parser.getLocalName().equals("name")){
String href = parser.getAttributeValue(null, "href");
if (href != null)
System.out.println(href);
}
}
}
}
}