[英]How to parse xml with namespace using android sax parser
我的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
?
正如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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.