title: java.xml.bind create XMLStreamReader from an external file
body: <p>The following code is used to unmarshall data from an XML resource in the classpath:</p>
  
  <pre><code>public static ConfigurationContext loadConfigurationContext() {
          ConfigurationContext config = null;
          try{
              JAXBContext jaxbContext = JAXBContext.newInstance(ConfigurationContext.class);
              Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
              XMLInputFactory xmlInputFactor = XMLInputFactory.newInstance();
              InputStream is = ConfigurationContext.class.getResourceAsStream("/com/hersa/testgateway/configuration/configuration.xml");
              XMLStreamReader xmlStreamReader = xmlInputFactor.createXMLStreamReader(is);
              config = (ConfigurationContext)unmarshaller.unmarshal(xmlStreamReader);
          } catch(Exception e){
              e.printStackTrace();
          }
          return config;
      }
  </code></pre>
  
  <p>I cannot seem to find a way to perform this same action using an external file.
  I need to be able to read from an external location like so. Notice the resource path.</p>
  
  <pre><code>public static ConfigurationContext loadConfigurationContext() {
          ConfigurationContext config = null;
          try{
              JAXBContext jaxbContext = JAXBContext.newInstance(ConfigurationContext.class);
              Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
              XMLInputFactory xmlInputFactor = XMLInputFactory.newInstance();
              File configfile = new File("C:\\Users\\me\\Documents\\TASKS\\configuration.xml");
              InputStream is = ConfigurationContext.class.getResourceAsStream(configfile.getAbsolutePath());
              XMLStreamReader xmlStreamReader = xmlInputFactor.createXMLStreamReader(is);
              config = (ConfigurationContext)unmarshaller.unmarshal(xmlStreamReader);
          } catch(Exception e){
              e.printStackTrace();
          }
          return config;
      }
  </code></pre>
  
  <p>I have also attempted to create a FileInputStream out of the XML file and passed it to the .createXMLStramReader() with no success.</p>
  
  <p>Any input will be appreciated.</p>
  
  <p>the code above gives me the follwoing trace:</p>
  
  <pre><code>[2/6/18 13:48:01:633 CST] 00000092 SystemErr     R javax.xml.stream.XMLStreamException: java.net.MalformedURLException
  [2/6/18 13:48:01:634 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.stax.msg.StAXMessageProvider.throwXMLStreamException(StAXMessageProvider.java:67)
  [2/6/18 13:48:01:634 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.stax.XMLStreamReaderImpl.setDocumentEntity(XMLStreamReaderImpl.java:392)
  [2/6/18 13:48:01:634 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.stax.XMLInputFactoryImpl.setDocumentEntity(XMLInputFactoryImpl.java:1440)
  [2/6/18 13:48:01:634 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.stax.XMLInputFactoryImpl.createXMLStreamReaderInternal0(XMLInputFactoryImpl.java:1455)
  [2/6/18 13:48:01:634 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.stax.XMLInputFactoryImpl.createXMLStreamReaderInternal(XMLInputFactoryImpl.java:1547)
  [2/6/18 13:48:01:634 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.stax.XMLInputFactoryImpl.createXMLStreamReader(XMLInputFactoryImpl.java:1624)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.hecorp.testgateway.bo.ConfigurationManager.loadConfigurationContext(ConfigurationManager.java:50)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.hecorp.testgateway.bo.ConfigurationManager.loadRequestedApplication(ConfigurationManager.java:30)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.hecorp.testgateway.servlet.GatewayFilter.doFilter(GatewayFilter.java:75)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:967)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1107)
  [2/6/18 13:48:01:635 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3980)
  [2/6/18 13:48:01:636 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
  [2/6/18 13:48:01:636 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1016)
  [2/6/18 13:48:01:636 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
  [2/6/18 13:48:01:636 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:287)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
  [2/6/18 13:48:01:637 CST] 00000092 SystemErr     R  at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
  [2/6/18 13:48:01:638 CST] 00000092 SystemErr     R  at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
  [2/6/18 13:48:01:638 CST] 00000092 SystemErr     R  at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
  [2/6/18 13:48:01:638 CST] 00000092 SystemErr     R  at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
  [2/6/18 13:48:01:638 CST] 00000092 SystemErr     R  at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
  [2/6/18 13:48:01:638 CST] 00000092 SystemErr     R  at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
  [2/6/18 13:48:01:638 CST] 00000092 SystemErr     R  at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
  [2/6/18 13:48:01:638 CST] 00000092 SystemErr     R Caused by: java.net.MalformedURLException
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at java.net.URL.&lt;init&gt;(URL.java:643)
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at java.net.URL.&lt;init&gt;(URL.java:506)
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at java.net.URL.&lt;init&gt;(URL.java:455)
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.util.URI.openStream(URI.java:599)
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.sax.impl.SAX2ParsedEntityFactory.readFromInputSource(SAX2ParsedEntityFactory.java:113)
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.sax.impl.SAX2ParsedEntityFactory.createParsedEntity(SAX2ParsedEntityFactory.java:76)
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at com.ibm.xml.xlxp2.api.stax.XMLStreamReaderImpl.setDocumentEntity(XMLStreamReaderImpl.java:389)
  [2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  ... 30 more
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R Caused by: java.lang.NullPointerException
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R  at java.net.URL.&lt;init&gt;(URL.java:548)
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R  ... 36 more
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R java.lang.NullPointerException
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R  at com.hecorp.testgateway.bo.ConfigurationManager.loadRequestedApplication(ConfigurationManager.java:30)
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R  at com.hecorp.testgateway.servlet.GatewayFilter.doFilter(GatewayFilter.java:75)
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
  [2/6/18 13:48:01:640 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:967)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1107)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3980)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1016)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
  [2/6/18 13:48:01:641 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
  [2/6/18 13:48:01:642 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
  [2/6/18 13:48:01:642 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
  [2/6/18 13:48:01:642 CST] 00000092 SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:287)
  [2/6/18 13:48:01:642 CST] 00000092 SystemErr     R  at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
  [2/6/18 13:48:01:642 CST] 00000092 SystemErr     R  at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
  [2/6/18 13:48:01:642 CST] 00000092 SystemErr     R  at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
  [2/6/18 13:48:01:643 CST] 00000092 SystemErr     R  at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
  [2/6/18 13:48:01:643 CST] 00000092 SystemErr     R  at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
  [2/6/18 13:48:01:643 CST] 00000092 SystemErr     R  at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
  [2/6/18 13:48:01:643 CST] 00000092 SystemErr     R  at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
  [2/6/18 13:48:01:643 CST] 00000092 SystemErr     R  at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
  [2/6/18 13:48:01:643 CST] 00000092 SystemErr     R  at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
  [2/6/18 13:48:01:644 CST] 00000092 SystemErr     R  at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
  </code></pre>
  
  <p>XML below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;configurationContext&gt;
      &lt;testApplication&gt;
          &lt;applicationName&gt;Project 1&lt;/applicationName&gt;
          &lt;url&gt;http://localhost:9081/project1/&lt;/url&gt;
          &lt;contextRoot&gt;project1&lt;/contextRoot&gt;
          &lt;timeout&gt;1800&lt;/timeout&gt;
          &lt;welcomeMessage&gt;External XML!!!&lt;/welcomeMessage&gt;
          &lt;question&gt;
              &lt;name&gt;email&lt;/name&gt;
              &lt;displayName&gt;Email&lt;/displayName&gt;
              &lt;questionText&gt;Enter your email address:&lt;/questionText&gt;
              &lt;dataType&gt;email&lt;/dataType&gt;
              &lt;disabled&gt;false&lt;/disabled&gt;
              &lt;allowedResponses&gt;
                  &lt;allowedResponse&gt;myemail@email.com&lt;/allowedResponse&gt;
                  &lt;allowedResponse&gt;test@email.com&lt;/allowedResponse&gt;
              &lt;/allowedResponses&gt;
          &lt;/question&gt;
          &lt;question&gt;
              &lt;name&gt;dateOfBirth&lt;/name&gt;
              &lt;displayName&gt;Date of Birth&lt;/displayName&gt;
              &lt;questionText&gt;Enter your DOB:&lt;/questionText&gt;
              &lt;dataType&gt;date&lt;/dataType&gt;
              &lt;disabled&gt;false&lt;/disabled&gt;
              &lt;allowedResponses&gt;
                  &lt;allowedResponse&gt;2000-11-28&lt;/allowedResponse&gt;
                  &lt;allowedResponse&gt;11/28/2000&lt;/allowedResponse&gt;
              &lt;/allowedResponses&gt;
          &lt;/question&gt;
          &lt;question&gt;
              &lt;name&gt;ipAddress&lt;/name&gt;
              &lt;displayName&gt;I.P. Address&lt;/displayName&gt;
              &lt;questionText&gt;Enter your I.P. address:&lt;/questionText&gt;
              &lt;dataType&gt;text&lt;/dataType&gt;
              &lt;disabled&gt;false&lt;/disabled&gt;
              &lt;allowedResponses&gt;
                  &lt;allowedResponse&gt;271.0.0.1&lt;/allowedResponse&gt;
                  &lt;allowedResponse&gt;192.168.1.1&lt;/allowedResponse&gt;
              &lt;/allowedResponses&gt;
          &lt;/question&gt;
      &lt;/testApplication&gt;
      &lt;testApplication&gt;
          &lt;applicationName&gt;Project 2&lt;/applicationName&gt;
          &lt;url&gt;http://localhost:9081/project2/&lt;/url&gt;
          &lt;contextRoot&gt;project2&lt;/contextRoot&gt;
          &lt;timeout&gt;1800&lt;/timeout&gt;
          &lt;welcomeMessage&gt;External XML!!!&lt;/welcomeMessage&gt;
          &lt;question&gt;
              &lt;name&gt;email&lt;/name&gt;
              &lt;displayName&gt;Email&lt;/displayName&gt;
              &lt;questionText&gt;Enter your email address:&lt;/questionText&gt;
              &lt;dataType&gt;email&lt;/dataType&gt;
              &lt;disabled&gt;false&lt;/disabled&gt;
              &lt;allowedResponses&gt;
                  &lt;allowedResponse&gt;myemail@email.com&lt;/allowedResponse&gt;
                  &lt;allowedResponse&gt;test@email.com&lt;/allowedResponse&gt;
              &lt;/allowedResponses&gt;
          &lt;/question&gt;
          &lt;question&gt;
              &lt;name&gt;dateOfBirth&lt;/name&gt;
              &lt;displayName&gt;Date of Birth&lt;/displayName&gt;
              &lt;questionText&gt;Enter your DOB:&lt;/questionText&gt;
              &lt;dataType&gt;date&lt;/dataType&gt;
              &lt;disabled&gt;false&lt;/disabled&gt;
              &lt;allowedResponses&gt;
                  &lt;allowedResponse&gt;2000-11-28&lt;/allowedResponse&gt;
                  &lt;allowedResponse&gt;11/28/2000&lt;/allowedResponse&gt;
              &lt;/allowedResponses&gt;
          &lt;/question&gt;
          &lt;question&gt;
              &lt;name&gt;ipAddress&lt;/name&gt;
              &lt;displayName&gt;I.P. Address&lt;/displayName&gt;
              &lt;questionText&gt;&lt;/questionText&gt;
              &lt;dataType&gt;ipaddress&lt;/dataType&gt;
              &lt;disabled&gt;true&lt;/disabled&gt;&gt;
              &lt;allowedResponses&gt;
                  &lt;allowedResponse&gt;192.168.1.1&lt;/allowedResponse&gt;
                  &lt;allowedResponse&gt;192.168.1.2&lt;/allowedResponse&gt;
              &lt;/allowedResponses&gt;
          &lt;/question&gt;
          &lt;validIPs&gt;
              &lt;ip&gt;0:0:0:0:0:0:0:1&lt;/ip&gt;
              &lt;ip&gt;192.168.1.1&lt;/ip&gt;
              &lt;ip&gt;192.168.1.2&lt;/ip&gt;
          &lt;/validIPs&gt;
      &lt;/testApplication&gt;
  &lt;/configurationContext&gt;
  </code></pre>