How to parse xml with namespace using android sax parser

ⅰ亾dé卋堺 提交于 2019-12-13 19:18:15

问题


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?


回答1:


Well as G_H pointed out the problem was I wasn't capitalizing "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);
    }
});


来源:https://stackoverflow.com/questions/7815526/how-to-parse-xml-with-namespace-using-android-sax-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!