No protocol malformed URL Exception thrown at me w

2019-09-02 02:08发布

When trying to parse an XML String I get a malformed URL exception thrown at me.

Here is the stack trace:

java.net.MalformedURLException: no protocol: <explanation>
<NodeExplanations>
    <IDAfterSkipProcessing>/Temporary/isMfs</IDAfterSkipProcessing>
    <NodeExplanation>
        <ID>/Temporary/isMfs</ID>
        <SkippedToIDForExplanationData>/Temporary/isMfs</SkippedToIDForExplanationData>
        <Value>false</Value>
        <Gist>Equals</Gist>
        <Scenario>NOT_EQUAL</Scenario>
        <Title>IsMfs</Title>
        <Phrase>
            <Text>isMfs</Text>
        </Phrase>
        <Question>
            <Text>isMfs</Text>
        </Question>
        <ExplanationText>
            <Text>We can't get any more details on </Text>
            <NodeName>
                <Text>isMfs</Text>
            </NodeName>
            <Text> right now.</Text>
        </ExplanationText>
        <InputNodes>
            <InputNodeEntry>
                <ID>/Return/ReturnData/IRS1040/IndividualReturnFilingStatusCd</ID>
                <Role>Value</Role>
                <Value>2</Value>
                <Type>CALCULATED_NODE</Type>
                <HasSubExplanations>false</HasSubExplanations>
            </InputNodeEntry>
            <InputNodeEntry>
                <ID>/Constants/IRS1040/FilingStatus/MarriedFilingSeparatelyCd</ID>
                <Role>Value</Role>
                <Value>3</Value>
                <Type>CONSTANT_NODE</Type>
                <HasSubExplanations>false</HasSubExplanations>
            </InputNodeEntry>
        </InputNodes>
        <Children>
            <ID>/Return/ReturnData/IRS1040/IndividualReturnFilingStatusCd</ID>
        </Children>
    </NodeExplanation>
</NodeExplanations>
</explanation>

    at java.net.URL.<init>(URL.java:585)
    at java.net.URL.<init>(URL.java:482)
    at java.net.URL.<init>(URL.java:431)
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)

I'm not sure why, the XML is definitely valid and not malformed?

Here is the code which is doing the parsing:

static Document getDocument(String xml) throws FileNotFoundException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    Document doc = null;

    try {
        db = dbf.newDocumentBuilder();
        doc = db.parse(xml);
        doc.getDocumentElement().normalize();
    } 
    catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    return doc;
}

What is causing this and how can I fix it? Any help or assistance would be much appreciated, thanks.

2条回答
成全新的幸福
2楼-- · 2019-09-02 02:10

A URI is required by DocumentBuilder.parse(String) method,the method then tries to open the URI. I you want to directly pass the content of the String, you have to give it as an InputStream.

DocumentBuilder db = ...;
String xml = ...;
db.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

This documentation might help http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html

查看更多
闹够了就滚
3楼-- · 2019-09-02 02:37

Figured out the problem was in fact the parse method.

Based on here: http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html

According to the documentation, the method parse(String uri), parses the content of the given URI as an XML document and return a new DOM Document object.

Solution would to be read it in as a byteStream

db.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

查看更多
登录 后发表回答