2011-04-18 69 views
0

我想从下面的xml中提取数据。率&货币工作正常,但我不能提取时间。从xml中提取数据的问题

<Cube> 
    <Cube time='2011-04-15'> 
     <Cube currency='USD' rate='1.4450'/> 
     <Cube currency='JPY' rate='120.37'/> 
    </Cube> 

中的startElement方法中的代码

if (localName.equals("Cube")) { 
      for (int i = 0; i < attributes.getLength(); i++) { 
       if ((attributes.getLocalName(i)).equals("currency")) { 
        name = attributes.getValue(i); 
       } else if ((attributes.getLocalName(i)).equals("time")) { 
        date = attributes.getValue(i); 
       } 
       else if ((attributes.getLocalName(i)).equals("rate")) { 
        try { 
         rate = Double.parseDouble(attributes.getValue(i)); 
        } catch (Exception e) { 
        } 
+0

这可能对你有帮助。 http://stackoverflow.com/questions/4827344/how-to-parse-xml-using-the-sax-parser/4828765#4828765 – 2011-04-18 13:00:33

回答

1

基于注解解析器是在这样的工作相当不错,我觉得Simple XML库可以处理这个要求。你应该检查它,因为它可以以更好的方式满足你的需求。

我甚至写了一篇关于如何在你的android项目中使用它的博客文章:which you can find here