Android解析xml错误-根元素名称不匹配
我想解析一个 gpx (xml) 文档,其开头如下:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" creator="Ian">
解析时出现以下错误:
android.sax.BadXmlException: Line 1: Root element name does not match. Expected: 'gpx', Got: 'http://www.topografix.com/GPX/1/1:gpx'
但是,如果我随后删除 xmlns="http://www.topografix.com/GPX/1/1 "
属性 - 它解析完美。
我用来解析的代码是 android.util.Xml.parse(is,Xml.Encoding.UTF_8,gpx.getContentHandler());
有谁知道为什么这个属性会导致解析错误?
非常感谢任何帮助! 伊恩
I want to parse an gpx (xml) document which starts as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" creator="Ian">
When parsing I get the follwoing error:
android.sax.BadXmlException: Line 1: Root element name does not match. Expected: 'gpx', Got: 'http://www.topografix.com/GPX/1/1:gpx'
However if I then remove the xmlns="http://www.topografix.com/GPX/1/1"
attribute - it parses perfectly.
The code I'm using to parse is android.util.Xml.parse(is,Xml.Encoding.UTF_8,gpx.getContentHandler());
Does anyone know why this attribute is cauing the parse error?
Any help is much appreciated!
Ian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您创建 rootElement 时,您有机会指定名称空间...
When you create your rootElement, you have an opportunity to specify the namespace...
它不是有效的 XML,我认为您只是在末尾缺少
,或者您可以通过在末尾执行
/>
自行关闭它。It is not valid XML, I think you are just missing a
</gpx>
at the end or you can just self close it by doing/>
at the end.您正在使用 xmlns 定义一个命名空间(因此您的所有标记名称都将以您在此属性中定义的任何内容开头),我猜架构无法识别该名称空间。
You're defining a namespace with the xmlns (so all your tag names will start with whatever you define in this attribute), which I guess the schema doesn't recognise.
我还没有弄清楚如何解决这个问题,但是如果其他人有同样的问题,您可以使用正则表达式从 xml 中删除 xmlns 属性。
这并不理想,因为您需要从整个 xml 文件创建一个字符串,这几乎违背了 sax 解析器的目的。如果有更好的想法,非常欢迎!
I still haven't worked out how to resolve the problem, however if anyone else has the same issue, you can use a regexp to remove the xmlns attribute from the xml.
This isn't ideal as you need to create a string out of your enitre xml file which almost defeats the point of a sax parser. Any better ideas are very welcome!