Java 如何使用 XStream 将对象列表转换为 XML 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4077071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert List of Object to XML doc using XStream
提问by
How to convert List of Object to XML doc using XStream ?
如何使用 XStream 将对象列表转换为 XML 文档?
and how to deserialize it back ?
以及如何反序列化它?
This is my xml
这是我的 xml
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<fullname>Guilherme</fullname>
<age>10</age>
<address>address,address,address,address,</address>
</person>
<person>
<fullname>Guilherme</fullname>
<age>10</age>
<address>address,address,address,address,</address>
</person>
</persons>
Person bean contains 3 fields how to convert back it to Bean List using custom converters ?
Person bean 包含 3 个字段,如何使用自定义转换器将其转换回 Bean List?
采纳答案by dogbane
You don't necessarily need a CustomConverter.
您不一定需要 CustomConverter。
You need a class to hold your list:
你需要一个类来保存你的列表:
public class PersonList {
private List<Person> list;
public PersonList(){
list = new ArrayList<Person>();
}
public void add(Person p){
list.add(p);
}
}
To serialise the list to XML:
要将列表序列化为 XML:
XStream xstream = new XStream();
xstream.alias("person", Person.class);
xstream.alias("persons", PersonList.class);
xstream.addImplicitCollection(PersonList.class, "list");
PersonList list = new PersonList();
list.add(new Person("ABC",12,"address"));
list.add(new Person("XYZ",20,"address2"));
String xml = xstream.toXML(list);
To deserialise xml to a list of person objects:
要将 xml 反序列化为人员对象列表:
String xml = "<persons><person>...</person></persons>";
PersonList pList = (PersonList)xstream.fromXML(xml);
回答by Martijn Verburg
Just use the std toXml and fromXml methods, see http://en.wikipedia.org/wiki/XStreamfor an example. Also see http://x-stream.github.io/converters.htmlon how the default conversions work.
只需使用 std toXml 和 fromXml 方法,请参阅http://en.wikipedia.org/wiki/XStream的示例。另请参阅http://x-stream.github.io/converters.html,了解默认转换的工作方式。
OK, so the default converters won't quite work in your case. You need to follow:
好的,所以默认转换器在您的情况下不太适用。您需要遵循:
回答by Boiling Epsilon
Load XML
加载 XML
public static Object Load(String xmlPath) throws Exception
{
File FileIn = new File(xmlPath);
if(FileIn.exists()) {
//Initialise Doc
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document Doc = builder.parse(FileIn);
//Initialise XPath
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
String objectClassLocation = xpath.evaluate("/object/@class",Doc);
Object ObjType;
//Create List of attributes for the Student
XPathExpression xpathExpression = xpath.compile("/object/*");
NodeList ObjTypeAttributes = (NodeList)xpathExpression.evaluate(Doc, XPathConstants.NODESET);
ObjType = CreateObject(ObjTypeAttributes, objectClassLocation);
return ObjType;
}
return null;
}
Create Object
创建对象
public static Object CreateObject(NodeList ObjectAttributes, String Location) throws Exception
{
Class ClassName = Class.forName(Location);
Object object = ClassName.newInstance();
Field[] fields = ClassName.getFields();
for(int x = 0; x < fields.length;x++)
{
for(int y = 0; y<ObjectAttributes.getLength(); y++)
{
if(!(ObjectAttributes.item(y) instanceof Text)) {
String check = ObjectAttributes.item(y).getAttributes().item(0).getNodeValue();
if(fields[x].getName().equals(check))
{
Field curField = ClassName.getField(fields[x].getName());
if(ObjectAttributes.item(y).getAttributes().getLength() < 2) {
curField.set(object,CreateList(ObjectAttributes.item(y).getChildNodes()));
}
else {
curField.set(object,ObjectAttributes.item(y).getAttributes().item(1).getNodeValue());
}
}
}
}
}
return object;
}
Create list (Only used if xml has an object of objects)
创建列表(仅在 xml 具有对象的对象时使用)
public static ArrayList CreateList(NodeList ArrayNodeList) throws Exception
{
ArrayList List = new ArrayList();
for(int x = 0; x < ArrayNodeList.getLength();x++)
{
if(!(ArrayNodeList.item(x) instanceof Text)) {
Node curNode = ArrayNodeList.item(x);
NodeList att = curNode.getChildNodes();
String Location = ArrayNodeList.item(x).getAttributes().item(0).getNodeValue();
Object newOne = CreateObject(att, Location);
List.add(newOne);
}
}
return List;
}
XML example I used
我使用的 XML 示例
<?xml version="1.0" encoding="UTF-8"?>
<object class="Example.Rps">
<field name="Representatives">
<object class="Example.Rep">
<field name="RepID" value="888225462"/>
<field name="Surname" value="Johnson"/>
<field name="Name" value="Dave"/>
<field name="Clients">
<object class="Example.Client">
<field name="ClientName" value="Cipla"/>
<field name="State" value="New York"/>
<field name="grade" value="A"/>
</object>
<object class="Example.Client">
<field name="ClientName" value="Pharmco"/>
<field name="State" value="Iowa"/>
<field name="grade" value="B"/>
</object>
</field>
</object>
<object class="Example.Rep">
<field name="RepID" value="888225462"/>
<field name="Surname" value="Dickson"/>
<field name="Name" value="Ben"/>
<field name="Clients">
<object class="Example.Client">
<field name="ClientName" value="XYZ"/>
<field name="State" value="New Mexico"/>
<field name="grade" value="A"/>
</object>
<object class="Example.Client">
<field name="ClientName" value="Pharmco"/>
<field name="State" value="Ohio"/>
<field name="grade" value="c"/>
</object>
</field>
</object>
</field>
</object>