Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用XStream将对象列表转换为XML文档_Java_Xml_Xstream - Fatal编程技术网

Java 如何使用XStream将对象列表转换为XML文档

Java 如何使用XStream将对象列表转换为XML文档,java,xml,xstream,Java,Xml,Xstream,如何使用XStream将对象列表转换为XML文档 如何反序列化它 这是我的xml <?xml version="1.0" encoding="UTF-8"?> <persons> <person> <fullname>Guilherme</fullname> <age>10</age> <address>address,address,address,address,</addr

如何使用XStream将对象列表转换为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>

吉列尔梅
10
地址,地址,地址,地址,
吉列尔梅
10
地址,地址,地址,地址,

Person bean包含3个字段,如何使用自定义转换器将其转换回bean列表?

只需使用std-toXml和fromXml方法,请参阅示例。另请参见有关默认转换如何工作的信息

好的,所以默认的转换器在您的情况下不太起作用。您需要遵循:


您不一定需要自定义转换器

您需要一个类来保存您的列表:

public class PersonList {

    private List<Person> list;

    public PersonList(){
        list = new ArrayList<Person>();
    }

    public void add(Person p){
        list.add(p);
    }
}
将xml反序列化到person对象列表:

    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);
    String xml = "<persons><person>...</person></persons>";
    PersonList pList = (PersonList)xstream.fromXML(xml);
stringxml=“…”;
PersonList pList=(PersonList)xstream.fromXML(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;
}
创建对象

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;

}
public静态对象CreateObject(节点列表ObjectAttributes,字符串位置)引发异常
{
Class ClassName=Class.forName(位置);
Object=ClassName.newInstance();
Field[]fields=ClassName.getFields();
for(int x=0;x对于(int y=0;yThanks,我已经更新了问题谢谢,但在这里我只是反序列化,我需要编写自己的转换器,我对列表转换感到困惑在您的示例中,我得到了一个错误:“@Marci man”隐式收集的“无字段”列表请注意
xstream.addImplicitCollection中的
List
(PersonList.class,“list”);
必须是
PersonList
的集合名,即
list
。Dobane,
xstream.alias
调用的目的是什么?是否可以用作通用对象序列化程序(类似于接受对象并将其保存为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 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>