简体   繁体   English

如何使用android sax解析器解析XML与名称空间

[英]How to parse xml with namespace using android sax parser

My XML Document looks like this: 我的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>

I'm able to get "An ID here" from the id tag with this code: 我可以使用以下代码从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>
    }
});

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 ? 我需要做什么才能得到d:Name

Well as G_H pointed out the problem was I wasn't capitalizing "name" 正如G_H指出的那样,问题是我没有将"name"大写

This works: 这有效:

Element name = properties.getChild(DATASERVICES_NAMESPACE, "Name"); // 'Name' instead of 'name'
name.setEndTextElementListener(new EndTextElementListener(){
    public void end(String body){
        messages.add(body);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM