URL 不存在时出现 SAXParseException
我正在尝试使用 DocumentBuilder
的 parse
方法解析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我更喜欢设置网络连接,或者自己打开文件并将流传递到 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.