java.xml.bind从外部文件创建XMLStreamReader

时间:2018-02-06 19:53:53

标签: java xml

以下代码用于解组类路径中XML资源的数据:

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;
    }

我似乎找不到使用外部文件执行相同操作的方法。 我需要能够从外部位置读取这样的内容。注意资源路径。

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;
    }

我还试图从XML文件中创建一个FileInputStream,并将其传递给.createXMLStramReader()但没有成功。

任何意见都将受到赞赏。

上面的代码为我提供了以下内容:

[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.<init>(URL.java:643)
[2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at java.net.URL.<init>(URL.java:506)
[2/6/18 13:48:01:639 CST] 00000092 SystemErr     R  at java.net.URL.<init>(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.<init>(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)

下面的XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configurationContext>
    <testApplication>
        <applicationName>Project 1</applicationName>
        <url>http://localhost:9081/project1/</url>
        <contextRoot>project1</contextRoot>
        <timeout>1800</timeout>
        <welcomeMessage>External XML!!!</welcomeMessage>
        <question>
            <name>email</name>
            <displayName>Email</displayName>
            <questionText>Enter your email address:</questionText>
            <dataType>email</dataType>
            <disabled>false</disabled>
            <allowedResponses>
                <allowedResponse>myemail@email.com</allowedResponse>
                <allowedResponse>test@email.com</allowedResponse>
            </allowedResponses>
        </question>
        <question>
            <name>dateOfBirth</name>
            <displayName>Date of Birth</displayName>
            <questionText>Enter your DOB:</questionText>
            <dataType>date</dataType>
            <disabled>false</disabled>
            <allowedResponses>
                <allowedResponse>2000-11-28</allowedResponse>
                <allowedResponse>11/28/2000</allowedResponse>
            </allowedResponses>
        </question>
        <question>
            <name>ipAddress</name>
            <displayName>I.P. Address</displayName>
            <questionText>Enter your I.P. address:</questionText>
            <dataType>text</dataType>
            <disabled>false</disabled>
            <allowedResponses>
                <allowedResponse>271.0.0.1</allowedResponse>
                <allowedResponse>192.168.1.1</allowedResponse>
            </allowedResponses>
        </question>
    </testApplication>
    <testApplication>
        <applicationName>Project 2</applicationName>
        <url>http://localhost:9081/project2/</url>
        <contextRoot>project2</contextRoot>
        <timeout>1800</timeout>
        <welcomeMessage>External XML!!!</welcomeMessage>
        <question>
            <name>email</name>
            <displayName>Email</displayName>
            <questionText>Enter your email address:</questionText>
            <dataType>email</dataType>
            <disabled>false</disabled>
            <allowedResponses>
                <allowedResponse>myemail@email.com</allowedResponse>
                <allowedResponse>test@email.com</allowedResponse>
            </allowedResponses>
        </question>
        <question>
            <name>dateOfBirth</name>
            <displayName>Date of Birth</displayName>
            <questionText>Enter your DOB:</questionText>
            <dataType>date</dataType>
            <disabled>false</disabled>
            <allowedResponses>
                <allowedResponse>2000-11-28</allowedResponse>
                <allowedResponse>11/28/2000</allowedResponse>
            </allowedResponses>
        </question>
        <question>
            <name>ipAddress</name>
            <displayName>I.P. Address</displayName>
            <questionText></questionText>
            <dataType>ipaddress</dataType>
            <disabled>true</disabled>>
            <allowedResponses>
                <allowedResponse>192.168.1.1</allowedResponse>
                <allowedResponse>192.168.1.2</allowedResponse>
            </allowedResponses>
        </question>
        <validIPs>
            <ip>0:0:0:0:0:0:0:1</ip>
            <ip>192.168.1.1</ip>
            <ip>192.168.1.2</ip>
        </validIPs>
    </testApplication>
</configurationContext>

1 个答案:

答案 0 :(得分:1)

解决方案:使用FileInputStream

public static ConfigurationContext loadConfigurationContext() {
    ConfigurationContext config = null;
    try{
        JAXBContext jaxbContext = JAXBContext.newInstance(ConfigurationContext.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory xmlInputFactor = XMLInputFactory.newInstance();
        InputStream is = new FileInputStream( "C:\\Users\\me\\Documents\\TASKS\\configuration.xml" );
        XMLStreamReader xmlStreamReader = xmlInputFactor.createXMLStreamReader(is);
        config = (ConfigurationContext)unmarshaller.unmarshal(xmlStreamReader);
    } catch(Exception e){
        e.printStackTrace();
    }
    return config;
}