URL 不存在时出现 SAXParseException

发布于 2024-12-21 10:54:12 字数 1368 浏览 0 评论 0原文

我正在尝试使用 DocumentBuilderparse 方法解析 xml,该方法将 URI 作为参数。为此,我创建一个表示 URI 的 String 对象,然后调用 parse 方法,将 String 对象作为参数传递。

parse 方法调用工作正常,返回一个新的 DOM 对象。然而,当我尝试打印返回的 DOM 对象时,它说:

org.xml.sax.SAXParseException:文档中根元素后面的标记必须格式正确。解析xml的代码片段如下所示:

String sURL="http://host:port/myapp/myfile.xml";   
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();      
doc = dBuilder.parse(sURL);

但是,当URL不存在时,parse方法仍然返回

org.xml.sax.SAXParseException:以下文档中的标记 根元素必须格式良好。

该 xml 是一个普通的 xml,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<Data>
    <ColorMessages>
        <Message Color="RED">Warning: The batch color level is red. Please check it out!</Message>
        <Message Color="YELLOW">Alert: This batch color level is yellow. Please stay on alert!</Message>
        <Message Color="GREEN">Cool: The batch color level is green. Nice!</Message>
        <Message Color="BLUE">Excellent: The batch color level is blue. Great job!</Message>
    </ColorMessages>
</Data>

关于如何在 parse 方法调用期间识别不存在的 URL 有什么想法吗?

谢谢。

I am trying to parse an xml using DocumentBuilder's parse method which takes URI as an argument. For this I create a String object representing the URI and then call the parse method passing the String object as the argument.

The parse method call works fine, returns a new DOM object. However, when I try to print the returned DOM object, it says:

org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed. The code snippet for parsing the xml is as shown below:

String sURL="http://host:port/myapp/myfile.xml";   
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();      
doc = dBuilder.parse(sURL);

However, when the URL is a non-existent one, the parse method still returns

org.xml.sax.SAXParseException: The markup in the document following
the root element must be well-formed.

The xml is a normal xml, something as shown below:

    <?xml version="1.0" encoding="utf-8"?>
<Data>
    <ColorMessages>
        <Message Color="RED">Warning: The batch color level is red. Please check it out!</Message>
        <Message Color="YELLOW">Alert: This batch color level is yellow. Please stay on alert!</Message>
        <Message Color="GREEN">Cool: The batch color level is green. Nice!</Message>
        <Message Color="BLUE">Excellent: The batch color level is blue. Great job!</Message>
    </ColorMessages>
</Data>

Any thoughts on how I could identify a non-existent URL during a parse method call?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2024-12-28 10:54:12

就我个人而言,我更喜欢设置网络连接,或者自己打开文件并将流传递到 xml 子系统。这需要更多的工作,但你可以控制他的进展。也就是说,在这种情况下,你不依赖于其他人认为是个好主意的想法。

执行此操作时,您还需要确保关闭流。下面的 XML 位并没有假设这就是您想要的。

Personally I prefer to set up the network connection, or open the file myself and just pass the stream to the xml sub-system. Its a bit more work, but you can control what his going on. i.e. you're not dependent upon what somebody else thought was a good idea in that situation.

When you do this, you also need to make sure you close the streams. The XML bits underneath don't make the assumption that that is what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文