如何使用 android sax 解析器解析带有命名空间的 xml
我的 XML 文档如下所示:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<feed xml:base="http://localhost/someApp" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<entry>
<id>An ID here</id>
<content type="application/xml">
<m:properties>
<d:Name>The name I want to get</d:Name>
</m:properties>
</content>
</entry>
</feed>
我可以使用以下代码从 id
标记获取“此处的 ID”:
String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
RootElement root = new RootElement(ATOM_NAMESPACE, "feed");
Element entry = root.getChild(ATOM_NAMESPACE, "entry");
Element id = entry.getChild(ATOM_NAMESPACE, "id");
id.setEndTextElementListener(new EndTextElementListener(){
public void end(String body){
messages.add(body); // messages = ArrayList<String>
}
});
但是,我似乎无法获取“我想要获取的名称” 。
我添加了这段代码:
String METADATA_NAMESPACE = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
String DATASERVICES_NAMESPACE = "http://schemas.microsoft.com/ado/2007/08/dataservices";
Element content = entry.getChild(ATOM_NAMESPACE, "content");
Element properties = content.getChild(METADATA_NAMESPACE, "properties");
Element name = properties.getChild(DATASERVICES_NAMESPACE, "name");
name.setEndTextElementListener(new EndTextElementListener(){
public void end(String body){
messages.add(body);
}
});
但消息列表中没有添加任何内容。
我需要做什么才能获得d:Name
?
My XML Document looks like this:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<feed xml:base="http://localhost/someApp" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<entry>
<id>An ID here</id>
<content type="application/xml">
<m:properties>
<d:Name>The name I want to get</d:Name>
</m:properties>
</content>
</entry>
</feed>
I'm able to get "An ID here" from the id
tag with this code:
String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
RootElement root = new RootElement(ATOM_NAMESPACE, "feed");
Element entry = root.getChild(ATOM_NAMESPACE, "entry");
Element id = entry.getChild(ATOM_NAMESPACE, "id");
id.setEndTextElementListener(new EndTextElementListener(){
public void end(String body){
messages.add(body); // messages = ArrayList<String>
}
});
However, I can't seem to get "The name I want to get".
I added this code:
String METADATA_NAMESPACE = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
String DATASERVICES_NAMESPACE = "http://schemas.microsoft.com/ado/2007/08/dataservices";
Element content = entry.getChild(ATOM_NAMESPACE, "content");
Element properties = content.getChild(METADATA_NAMESPACE, "properties");
Element name = properties.getChild(DATASERVICES_NAMESPACE, "name");
name.setEndTextElementListener(new EndTextElementListener(){
public void end(String body){
messages.add(body);
}
});
but nothing gets added the messages list.
What do I need to do in order to get d:Name
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 G_H 指出的,问题是我没有大写
"name"
这有效:
Well as G_H pointed out the problem was I wasn't capitalizing
"name"
This works: