2015-12-21 59 views
0

我正在使用XmlPullParser来读取xml文件。 我的文件中有如下一行:Android:使用XMLPullParser的getname()

<Circle> 
<Circle time="2015-12-21"> 

而我的问题是,在第二行,因为我使用getname()但它只返回Circle,而不是返回Circle time="2015-12-21"

我的代码:

URL url = new URL(site); 
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
factory.setNamespaceAware(false); 
XmlPullParser xpp = factory.newPullParser() 

while (xpp.next() != XmlPullParser.END_DOCUMENT) { 
    if (xpp.getEventType() != XmlPullParser.START_TAG) {   
     continue; 
    } 
     name = xpp.getName(); 

    if (name.equals("Circle time=\""+getDate()+"\"")) { 
     Log.d("Example","Success!!"); 
    } 
} 

name总是Circle,而不是Circle time="2015-12-21"

你能帮我吗?

回答

0

时间是属性。要检索其值,可以使用 getAttributeValue(java.lang.String, java.lang.String)getName返回当前标签的名称。

String name = xpp.getName(); 
    if ("Circle".equals(name)) { 
     String time = xpp.getAttributeValue(null, "time"); 
     // here time contains the content of the attribute 
     // you can compare it with getDate(); 
    } 
+0

当我做Log.d(“例子”,“时间:”+时间)里面,如果你写它返回我那个时间是空的......我做错了吗? – porthfind

+0

以及它取决于你的XML的结构。你没有时间发布一个标签圈,另一个圈。 – Blackbelt

+0

是的,这就是为什么我要比较''立方体时间= \“”+ getDate()+“\”“',要输入第二个标签 – porthfind