I'm trying to deserialize an xml string with SimpleXML, i've looked at their examples but i'm not really sure that wether i grasp the concept or not.
Sample XML (validates):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://localhost/webservices/">
<Result>
<Item><ID>0</ID><language /><price>168</price></Item>
<Item><ID>1</ID><language /><price>178</price></Item>
<Item><ID>2</ID><language /><price>195</price></Item>
<Item><ID>3</ID><language /><price>169</price></Item>
<Item><ID>4</ID><language /><price>178</price></Item>
<Item><ID>5</ID><language /><price>178</price></Item>
<Item><ID>6</ID><language /><price>149</price></Item>
<Item><ID>7</ID><language /><price>178</price></Item>
<Item><ID>8</ID><language /><price>168</price></Item>
<Item><ID>9</ID><language /><price>179</price></Item>
<Item><ID>10</ID><language /><price>147</price></Item>
<Item><ID>11</ID><language /><price>165</price></Item>
<Item><ID>12</ID><language /><price>192</price></Item>
<Item><ID>13</ID><language /><price>218</price></Item>
<Item><ID>14</ID><language /><price>144</price></Item>
<Item><ID>15</ID><language /><price>141</price></Item>
</Result>
</Response>
</soap:Body></soap:Envelope>
And the java code:
@Root(name="Result",strict=false)
public class ItemList {
@ElementList(entry="Item")
private List<Item> _list;
public List<Book> GetItemList()
{
return _list;
}
public void SetItemList(List<Item> value)
{
this._list = value;
}
}
@Root(strict=false)
public class Item {
@Element(name="ID")
private String _ID;
@Element(name="price")
private String _price;
public String GetPrice()
{
return _price;
}
public void SetPrice(String value)
{
this._price = value;
}
public String GetID()
{
return _ID;
}
public void SetID(String value)
{
this._ID = value;
}
public Item(String ID,
String price)
{
this._ID = ID;
this._price = price;
}
}
Any help is appreciated.
I have a suggestion, but it's not ready to run (see below). However, ther maybe another, better solution ...
Class Item
Holding all your informations.
@Root(name="Item")
public class Item
{
@Element(name="ID", required=true)
private int id;
@Element(name="language", required=true)
private String language;
@Element(name="price", required=true)
private int price;
// ...
}
Class Result
Constructing everything around Item
. Btw. you dont have to use inner classes here.
@Namespace(prefix="soap", reference="http://schemas.xmlsoap.org/soap/envelope/")
@Root(name="Envelope")
public class Result
{
@Namespace(prefix="soap")
@Element(name="Body")
private SoapBody body;
// ...
// -----------------------------------------------------------------
// -- Some inner classes, constructing the elements as in you xml --
// -----------------------------------------------------------------
@Namespace(prefix="soap")
@Root(name="Body")
static class SoapBody
{
@Element(name="Response")
private Response response;
// ...
}
@Root(name="Response")
static class Response
{
@ElementList(name="Result", required=true)
private List<Item> result;
// ...
}
}
(Example) How to use this code
Writing
File f = ...
Serializer ser = new Persister();
Result r = new Result();
ser.write(r, f);
Reading
File f = ...
Serializer ser = new Persister();
Result r = ser.read(Result.class, f);
Now ... there's one problem which prevents this example from running: <language />
This empty Element let SimpleXML throw a ValueRequiredException
.
@Element( required=false)
private String language;
Add this in your Item class and generate the getter and setter.I think it should work
来源:https://stackoverflow.com/questions/13155056/deserialization-of-xml-with-simplexml-in-java