English 中文(简体)
如何将复合物体的ArrayList储存到xml?
原标题:How can i store the ArrayList of complex objects into xml?

I have two classes which are having relations with each other as below. I have a list of ObjReal objects where the whole data is presented. I want to save the whole List into xml without losing the object relationships. I mean an object represented in XML should have their corresponding objStrucs along with it. To store ObjReal into xml is straight forward but i am getting confused with its ObjStruc relations. Please helpe to solve this problem.

My ObjReal是:

Class ObjReal
{
  private String id;
  private String data;

  ArrayList<ObjStruc> objStrucs=new ArrayList<ObjStruc>();

  public ArrayList<ObjStruc> getObjStrucs()
    {
        return objStrucs;
    }

  public String getId()
 {
      return id;
 }

  public String getData()
    {
      return data;
    }

    public void setId(String id)
    {
        this.id=id;
    }
    public void setData(String data)
    {
        this.data=data;
    }

}

My ObjStruc是:

Class ObjStruc
{
    private ObjReal objReal;
    public ObjReal getObjReal()
 {
      return objReal;
 }

  public ObjReal setObjReal(ObjReal objReal)
    {
      this.objReal=objReal;
    }

}

完整的数据载于<代码>。 ArrayList<ObjReal> obreals Object which i would i consider into xml. 我希望我保持明确的观点。 提前感谢。

问题回答

<>说明: I m the EclipseLink JAXB (MOXy) http://jcp.org/en/jsr/detail?id=222"rel=“nofollow noreferer”JAXB 2 (JSR-222) 专家组。

您的使用情况有两个有趣方面:

  1. ArrayList as the root object
  2. Bidirectional relationship between ObjReal and ObjStruc

1. ARRAYLIST AS ROOT OBJECT

宇宙航空研究开发机构(MOXy、Metro、JaxMe等)的实施不能直接支持处理<条码>、Collection作为根基物体的类型。 为了处理这一使用案例,你只是需要建立一个有希望的<条码>的包装类别。

@XmlRootElement(name="root-element-name")
@XmlAccessorType(XmlAccessType.FIELD)
public class ListWrapper {

    private ArrayList<ObjReal> objReals;        

}

2.BIDIRECTIONAL RelatIOHIP

您可使用<代码>@Xml InverseReference,将日本宇宙航空研究开发机构的EclipseLink付诸实施,处理你模式中的双向关系。 我举了一个完整的例子。 我略去了大部分用于节省空间的通道。

www.un.org/Depts/DGACM/index_spanish.htm ObjReal

import java.util.ArrayList;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class ObjReal {

  private String id;
  private String data;
  ArrayList<ObjStruc> objStrucs=new ArrayList<ObjStruc>();

  public ArrayList<ObjStruc> getObjStrucs() {
    return objStrucs;
  }

}

<><>ObjStruc>

The Xml InverseReference annotation is used on this category. 在这项说明中,你具体说明了关系的其他方向的假名/财产名称:

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@XmlAccessorType(XmlAccessType.FIELD)
class ObjStruc {

    @XmlInverseReference(mappedBy="objStrucs")
    private ObjReal objReal;

    public ObjReal getObjReal() {
        return objReal;
    }

}

<>strong>jaxb.properties

• 指定MOXy为您的日本宇宙航空研究开发机构提供商,需要将一份名为jaxb.properties的档案列入与贵领域类别相同的成套材料:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

你们可以操作以下法典,以核实地图:

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception  {
        JAXBContext jc = JAXBContext.newInstance(ObjReal.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum8868303/input.xml");
        ObjReal objReal = (ObjReal) unmarshaller.unmarshal(xml);

        for(ObjStruc objStruc : objReal.getObjStrucs()) {
            System.out.println(objStruc.getObjReal());
        }

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(objReal, System.out);
    }

}

Input (input.xml)

<?xml version="1.0" encoding="UTF-8"?>
<objReal>
    <id>123</id>
    <data>some data</data>
    <objStrucs/>
    <objStrucs/>
</objReal>

Output

下面是代号法典的输出。 参看<代码>objReal,每份<代码>上的财产 奥杰斯里特( 物体在非婚生期间有人居住:

forum8868303.ObjReal@7f712b3a
forum8868303.ObjReal@7f712b3a
<?xml version="1.0" encoding="UTF-8"?>
<objReal>
   <id>123</id>
   <data>some data</data>
   <objStrucs/>
   <objStrucs/>
</objReal>

<>更多信息>

<><>Download EclipseLink>

You can download EclipseLink at:

有几个图书馆将序列分析物体结构以xml。 其中之一是XStream。 http://xstream.codehaus.org/tutorial.html“rel=“nofollow”>here。

Just feed it to XStream! It works both ways. Code snippets here. Good luck!

任何轻率地写一只 j子到xml的手法是使用java xml encoder。 这把全部物体和属地写到xml格式。 然后,你也可以把他们重新读到你们的物体上。

public static boolean writeXMLFile(final Object data, final String fileName) {

    XMLEncoder encoder = null;

    boolean result = true;

    try {
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(fileName)));
        encoder.writeObject(data);
    } catch (final IOException e) {
        logger.error(e.getMessage());
        result = false;
    } finally {
        if (encoder != null) {
            encoder.close();
        } else {
            result = false;
        }
    }

public static Object readXMLFile(final String fileName) {

    XMLDecoder decoder = null;
    Object data = null;

    try {
        FileInputStream fis = new FileInputStream(fileName);
        BufferedInputStream bis = new BufferedInputStream(fis);
        decoder = new XMLDecoder(bis);
        data = decoder.readObject();
    } catch (final FileNotFoundException e) {
        data = null;
        logger.error(e.getMessage());
    } catch (final Exception e) {
        data = null;
        logger.error(e.getMessage());
    } finally {
        if (decoder != null) {
            decoder.close();
        } else {
            data = null;
        }
    }

    return data;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签