开发者

How would I extract this XML Data?

开发者 https://www.devze.com 2023-01-28 01:24 出处:网络
I have the following XML output: <info> <ip>70.56.98.195</ip> <host>70-56-98-195.slkc.qwest.net</host>

I have the following XML output:

<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="Robt开发者_C百科ex"></ips> 
</searches>

My question is, what is the best way to pull that data out and is there a better way I should be out putting my XML data?


An pretty good tool for this is Simple. A you would have to do is write a simple object to serialize the data in to. For example.

@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;
   }
}

Then all you would have to do is read the data in to an object instance.

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

This framework works for just about all Android versions. For further information there is a Tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消