【问题标题】:How would I extract this XML Data?我将如何提取此 XML 数据?
【发布时间】:2010-11-29 23:15:24
【问题描述】:

我有以下 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 数据?

【问题讨论】:

  • 您写了 “有没有更好的方法可以输出我的 XML 数据?”。一个格式良好的 XML 文档怎么样?! :)

标签: java android xml parsing extract


【解决方案1】:

一个很好的工具是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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多