public class XmlUtil
{
- public static Document parse(InputStream is)
- throws SAXException, IOException
- {
- try {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setValidating(false);
- factory.setExpandEntityReferences(false);
- DocumentBuilder builder = factory.newDocumentBuilder();
- builder.setEntityResolver(new EntityResolver() {
- public InputSource resolveEntity(String publicId, String systemId)
- throws SAXException, IOException
- {
- return new InputSource(new StringReader(""));
- }});
- return builder.parse(is);
- }
- catch (ParserConfigurationException e) {
- throw new AssertionError("SAX/DOM parser configuration error");
- }
- }
-
- public static <E extends Node> Iterable<E> iterate(final NodeList nodes, final Class<E> type)
- {
- return new Iterable<E>() {
- public Iterator<E> iterator() {
- return new NodeListIterator<E>(nodes, type);
- }
- };
- }
-
- static class NodeListIterator<E extends Node>
- implements Iterator<E>
- {
- Class<E> nodeType_;
- NodeList nodes_;
- int i_ = 0;
- E next_;
-
- public NodeListIterator(NodeList nodes, Class<E> nodeType)
- {
- nodes_ = nodes;
- nodeType_ = nodeType;
- advance();
- }
-
- @Override
- public boolean hasNext()
- {
- return next_ != null;
- }
+ public static Document parse(InputStream is) throws SAXException, IOException
+ {
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setValidating(false);
+ factory.setExpandEntityReferences(false);
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ builder.setEntityResolver(new EntityResolver() {
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ return new InputSource(new StringReader(""));
+ }
+ });
+ return builder.parse(is);
+ }
+ catch (ParserConfigurationException e) {
+ throw new AssertionError("SAX/DOM parser configuration error");
+ }
+ }
- @Override
- public E next()
- {
- E rv = next_;
- advance();
- return rv;
- }
+ public static <E extends Node> Iterable<E> iterate(final NodeList nodes, final Class<E> type)
+ {
+ return new Iterable<E>() {
+ public Iterator<E> iterator()
+ {
+ return new NodeListIterator<E>(nodes, type);
+ }
+ };
+ }
- @Override
- public void remove()
- {
- throw new UnsupportedOperationException();
- }
-
- @SuppressWarnings("unchecked")
- void advance()
- {
- while (i_ < nodes_.getLength()) {
- Node n = nodes_.item(i_);
- ++ i_;
- if (nodeType_.isInstance(n)) {
- next_ = (E) n;
- return;
- }
- }
- next_ = null;
- }
- }
+ static class NodeListIterator<E extends Node> implements Iterator<E>
+ {
+ Class<E> nodeType_;
+ NodeList nodes_;
+ int i_ = 0;
+ E next_;
+
+ public NodeListIterator(NodeList nodes, Class<E> nodeType)
+ {
+ nodes_ = nodes;
+ nodeType_ = nodeType;
+ advance();
+ }
+
+ @Override
+ public boolean hasNext()
+ {
+ return next_ != null;
+ }
+
+ @Override
+ public E next()
+ {
+ E rv = next_;
+ advance();
+ return rv;
+ }
+
+ @Override
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @SuppressWarnings("unchecked")
+ void advance()
+ {
+ while (i_ < nodes_.getLength()) {
+ Node n = nodes_.item(i_);
+ ++i_;
+ if (nodeType_.isInstance(n)) {
+ next_ = (E) n;
+ return;
+ }
+ }
+ next_ = null;
+ }
+ }
}