How can I store an ArrayList of complex objects in xml?

I have two classes that are related to each other as shown below. I have a list of ObjReal objects where all the data is presented. I want to save the whole list in xml without losing the relationship to the object. I mean, an object represented in XML must have its corresponding objects with it. To save ObjReal in xml directly, but I get confused with its ObjStruc relationship. Please help 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;
    }

}

And all the data is in the object ArrayList<ObjReal> obrealsthat I want to dump in xml. Hope I have a clear moment. Thanks in advance.

+5
source share
5 answers

. EclipseLink JAXB (MOXy) JAXB 2 (JSR-222).

:

  • ArrayList
  • ObjReal ObjStruc

1. ARRAYLIST AS ROOT OBJECT

JAXB (MOXy, Metro, JaxMe ..) Collection . -, Collection /.

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

    private ArrayList<ObjReal> objReals;        

}

2. BIDIRECTIONAL RELATIOSNHIP

@XmlInverseReference EclipseLink JAXB . . , .

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

@XmlInverseReference. fied/property :

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

}

jaxb.properties

MOXy JAXB, 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.xml)

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

. , ObjReal ObjStruc :

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

EclipseLink

EclipseLink :

+4

, xml. XStream. .

+3

XStream! . . !

+2

Any easy way to write a java object for xml is to use a java-xml encoder. This writes your entire object and its dependencies to xml format. Then you can also read them back to your object.

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;
}
+1
source

All Articles