title: Convert Java w3c Document to XMLStreamReader
body: <p>I would like to reuse some existing code in our code base that accepts an <code>XMLStreamReader</code> my application has the required data as a w3c Document.</p>
  
  <p>The following example is a minimum test case:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = builderFactory.newDocumentBuilder();
  
      Document doc = builder.newDocument();
  
      Element rootElement = doc.createElement("Groups");
      doc.appendChild(rootElement);
      Element group = doc.createElement("Group");
      group.setTextContent("Wibble");
      rootElement.appendChild(group);
  
      DOMSource source = new DOMSource(doc);
  
      XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(source);
  
      reader.nextTag();
      System.out.println("NextTag:" + reader.getName());
  }
  </code></pre>
  
  <p>The expected output should be something like: <code>NextTag:Groups</code> but instead the following is thrown:</p>
  
  <pre><code>Exception in thread "main" javax.xml.stream.XMLStreamException: java.net.MalformedURLException
      at com.sun.xml.stream.XMLReaderImpl.setInputSource(XMLReaderImpl.java:196)
      at com.sun.xml.stream.XMLReaderImpl.&lt;init&gt;(XMLReaderImpl.java:179)
      at com.sun.xml.stream.ZephyrParserFactory.createXMLStreamReader(ZephyrParserFactory.java:139)
      at Main.main(Main.java:27)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  Caused by: java.net.MalformedURLException
      at java.net.URL.&lt;init&gt;(URL.java:601)
      at java.net.URL.&lt;init&gt;(URL.java:464)
      at java.net.URL.&lt;init&gt;(URL.java:413)
      at com.sun.xml.stream.XMLEntityManager.startEntity(XMLEntityManager.java:762)
      at com.sun.xml.stream.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:697)
      at com.sun.xml.stream.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:300)
      at com.sun.xml.stream.XMLReaderImpl.setInputSource(XMLReaderImpl.java:193)
      ... 8 
  </code></pre>
  
  <p>Currently using Java 6 update 22.</p>
  
  <p>Further info: The source to <a href="http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules/sjsxp/com/sun/xml/stream/ZephyrParserFactory.java.htm" rel="nofollow">ZephyrParserFactory#jaxpSourcetoXMLInputSource</a> seems to indicate that the Source object is converted by coping it's <code>SystemId</code> rather than the actual contents of the DOMSource.</p>
  
  <p><strong>Update:</strong> My orignal test case above was actually run using my project classpath which actually includes the JAXB 2.2.1 library which in turn pulls in sjsxp 1.0.1. Running on a clean classpath yields:</p>
  
  <pre><code>Exception in thread "main" java.lang.UnsupportedOperationException: Cannot create XMLStreamReader or XMLEventReader from a javax.xml.transform.dom.DOMSource
      at com.sun.xml.internal.stream.XMLInputFactoryImpl.jaxpSourcetoXMLInputSource(XMLInputFactoryImpl.java:302)
      at com.sun.xml.internal.stream.XMLInputFactoryImpl.createXMLStreamReader(XMLInputFactoryImpl.java:145)
  </code></pre>
  
  <p>Which fits with @Gary Rowe's answer.</p>