2016-08-16 65 views
0

我嘗試使用xstream與下面的代碼解析下面的xml。但我不斷收到解析異議。使用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); 

類:

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類:

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

下面是客戶端類中使用的類的詳細信息。

InfoIf類:

public interface InfoIf { 

    public String name(); 

    public String toString(); 
} 
+0

'ipAddresses'在哪裏初始化?我可以看到它被聲明和使用,但是看不到任何它的值被填充。 –

+0

IPAddressEntry表示ipAddresses – mohan

回答

0

與下面的代碼解決的問題。

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