gpt4 book ai didi

java - java中用simplexml反序列化xml

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:05:15 24 4
gpt4 key购买 nike

我正在尝试使用 SimpleXML 反序列化 xml 字符串,我看过他们的示例,但我不确定我是否掌握了这个概念。

示例 XML(验证):

<?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>

Java 代码:

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

感谢任何帮助。

最佳答案

我有一个建议,但还没有准备好运行(见下文)。然而,也许还有另一个更好的解决方案......

类项目

保存您的所有信息。

@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)成绩

围绕Item构建一切.顺便提一句。你不必在这里使用内部类。

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


// ...
}
}

(示例)如何使用此代码

写作

File f = ...

Serializer ser = new Persister();
Result r = new Result();
ser.write(r, f);

阅读

File f = ...

Serializer ser = new Persister();
Result r = ser.read(Result.class, f);

现在……有一个问题阻止了这个例子的运行:<language />这个空元素让 SimpleXML 抛出一个 ValueRequiredException .

关于java - java中用simplexml反序列化xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13155056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com