3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringReader;
6 import java.util.Iterator;
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16 import org.xml.sax.EntityResolver;
17 import org.xml.sax.InputSource;
18 import org.xml.sax.SAXException;
22 public static Document parse(InputStream is)
23 throws SAXException, IOException
26 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
27 factory.setValidating(false);
28 factory.setExpandEntityReferences(false);
29 DocumentBuilder builder = factory.newDocumentBuilder();
30 builder.setEntityResolver(new EntityResolver() {
31 public InputSource resolveEntity(String publicId, String systemId)
32 throws SAXException, IOException
34 return new InputSource(new StringReader(""));
36 return builder.parse(is);
38 catch (ParserConfigurationException e) {
39 throw new RuntimeException("SAX/DOM parser configuration error");
43 public static Iterable<Element> iterateElements(final NodeList nodes)
45 return new Iterable<Element>() {
46 public Iterator<Element> iterator() {
47 return new NodeListIterator<Element>(nodes, Element.class);
52 static class NodeListIterator<E extends Node>
53 implements Iterator<E>
60 public NodeListIterator(NodeList nodes, Class<?> nodeType)
68 public boolean hasNext()
84 throw new UnsupportedOperationException();
87 @SuppressWarnings("unchecked")
90 while (i_ < nodes_.getLength()) {
91 Node n = nodes_.item(i_);
93 if (nodeType_.isInstance(n)) {