java - 我将如何提取此 XML 数据?

标签 java android xml parsing extract

我有以下 XML 输出:

<info> 
  <ip>70.56.98.195</ip> 
  <host>70-56-98-195.slkc.qwest.net</host> 
  <country>UNITED STATES</country> 
  <cimg>http://localhost/ip-to-country/country-flags/us.png</cimg> 
</info> 
<searches> 
  <ips link="http://www.stopforumspam.com/search?q=70.56.98.195" title="Stop Forum Spam"></ips> 
  <ips link="http://openrbl.org/client/#70.56.98.195" title="Openrbl DNSBL RBL Blacklist"></ips> 
  <ips link="http://www.afrinic.net/cgi-bin/whois?searchtext=70.56.98.195" title="AfriNIC (Africa)"></ips> 
  <ips link="http://www.apnic.net/apnic-bin/whois2.pl?searchtext=70.56.98.195" title="APNIC (Asia Pacific region)"></ips>  
  <ips link="http://ws.arin.net/cgi-bin/whois.pl?queryinput=70.56.98.195" title="ARIN (North America, a portion of the Caribbean and sub-Saharan Africa)"></ips> 
  <ips link="http://lacnic.net/cgi-bin/lacnic/whois?query=70.56.98.195" title="LACNIC (Latin American and Caribbean region)"></ips> 
  <ips link="http://www.ripe.net/perl/whois?searchtext=70.56.98.195" title="RIPE (Europe, the Middle East and parts of Africa and Asia)"></ips> 
  <ips link="http://www.robtex.com/ip/70.56.98.195.html" title="Robtex"></ips> 
</searches>

我的问题是,提取该数据的最佳方法是什么?是否应该有更好的方法来输出 XML 数据?

最佳答案

一个非常好的工具是 Simple 。您要做的就是编写一个简单的对象来序列化数据。例如。

@Default
private class Structure {

   @Path("info")
   private String ip;

   @Path("host")
   private String host;

   @Path("path")
   private String country;

   @Path("path")
   private String cimg;

   @ElementList
   private List<Entry> searches;

   @Root
   private static class Entry {

      @Attribute
      private String link;

      @Attribute 
      private String title;
   }
}

然后您所要做的就是将数据读入对象实例。

Serializer serializer = new Persister();
Structure structure = serializer.read(Structure.class, inputStream);

该框架几乎适用于所有 Android 版本。欲了解更多信息,请访问 Tutorial .

关于java - 我将如何提取此 XML 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4309071/

相关文章:

android - 如何从 MediaStore 获取音乐的 URI?

javascript - Phonegap构建android禁用主页/电源/软键(信息亭模式类型应用程序)

xml - 服务器标记在 xml 内容上留下空行

xml - Golang HTTP XML 解析问题

java - 在 JPA/Hibernate 中使用 Spring 定义的 transactionManager

java - DAO 插入行为似乎不正常

java - 如何使用 RestTemplate 转发大文件?

java - SQL - DB2 中的 UPPER 函数不起作用

android - 发送 gcm 消息的错误请求

xml - 处理 XSLT 函数中的空序列