尝试解析XML字符串时,没有出现协议格式错误的URL异常

时间:2015-10-21 04:30:24

标签: java xml malformedurlexception

在尝试解析XML String时,我收到了一个格式错误的URL异常。

这是堆栈跟踪:

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)

我不确定为什么,XML绝对有效且没有格式错误?

以下是进行解析的代码:

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

造成这种情况的原因是什么?如何解决?非常感谢任何帮助或帮助,谢谢。

2 个答案:

答案 0 :(得分:2)

TextAsset textAsset = Resources.Load ("XML/level1.xml") as TextAsset; xml.LoadXml(textAsset.text); level_root_node = xml.DocumentElement; 方法需要URI,然后该方法尝试打开URI。我想直接传递String的内容,你必须把它作为一个InputStream。

DocumentBuilder.parse(String)

此文档可能有所帮助 http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html

答案 1 :(得分:0)

想出问题实际上是解析方法。

基于此处:http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html

根据文档,方法parse(String uri)将给定URI的内容解析为XML文档并返回一个新的DOM Document对象。

解决方案将作为byteStream

读取

db.parse(new InputSource(new ByteArrayInputStream(xml.getBytes(&#34; utf-8&#34;))));