如何使用的Android SAX解析器解析命名空间的xml

问题描述:

我的XML文档看起来是这样的:如何使用的Android SAX解析器解析命名空间的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

+1

在你使用'“名”'的代码,而XML具有标签当地名称为'Name'。这是大写字母吗? XML区分大小写。 –

好视G_H指出,问题是我没有资本"name"

这工作:

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