2016-08-16 4 views
0

Ich versuche, die unten xml mit Xstream mit dem folgenden Code zu analysieren. Aber ich bekomme parse Ausnahmesituationen.Parsing für geschachtelte Tabelle mit Xstream

<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='/getIpTable/getIpTable.xsd'> 
     <IPAddressInfo> 
      <ipAddrEntry> 
       <ipAdEntIfIndex>607</ipAdEntIfIndex> 
       <ipAdEntAddr>172.23.218.126</ipAdEntAddr> 
       <ipAdEntNetMask>255.255.255.192</ipAdEntNetMask> 
      </ipAddrEntry> 
     </IPAddressInfo> 
    </result> 



XStream xStream = new XStream(); 
     xStream.alias("result", IPAddressInfo.class); 
     xStream.alias("ipAddrEntry", Entry.class); 
     xStream.alias("ipAdEntIfIndex", String.class); 
     xStream.alias("ipAdEntAddr", String.class); 
     xStream.alias("ipAdEntNetMask", String.class); 
     IPAddressInfo ipAddressInfo = new IPAddressInfo(); 
     ipAddressInfo=(IPAddressInfo) xStream.fromXML(xml); 
     System.out.println("------"+ipAddressInfo); 

Klassen:

public class IPAddressInfo implements java.io.Serializable, InfoIf { 
    public IPAddressEntry[] ipAddresses; 

    public String name() { return "IPAddressInfo"; }; 

    public String toString() { 
     String rc = new String("IPAddressInfo collected:\n"); 
     for (int i = 0; ipAddresses != null && i < ipAddresses.length; i++) { 
      rc += " entry" + i + "------------------------------------\n"; 
      rc += " ifIndex = " + ipAddresses[i].ifIndex + "\n"; 
      rc += " IP  = " + ipAddresses[i].ip.getHostAddress() + "\n"; 
      rc += " ipMask = " + ipAddresses[i].ipMask.getHostAddress() + "\n"; 
     } 
     return rc; 
    } 
} 

IPAddressEntry Klasse:

public class IPAddressEntry implements java.io.Serializable { 
    public int ifIndex; 
    public InetAddress ip; 
    public InetAddress ipMask; 
} 

Im Folgenden sind die Klassen Details in der Client-Klasse verwendet.

InfoIf Klasse:

public interface InfoIf { 

    public String name(); 

    public String toString(); 
} 
+0

Wo wird 'ipAddresses' initialisiert? Ich kann sehen, dass es deklariert und verwendet wird, aber nichts sehen kann, seine Werte sind aufgefüllt. –

+0

IPAddressEntry repräsentiert die ipAddresses – mohan

Antwort

0

Das Problem mit folgendem Code gelöst.

XStream xstream = new XStream(new DomDriver()); 
     xstream.alias("ipAddressInfo", IPAddressInfo.class); 
     xstream.alias("ipAddrEntry", IPAddressEntry.class); 
     xstream.addImplicitCollection(IPAddressInfo.class, "ipAddresses");