简体   繁体   中英

Define XML Structure with Xstream

I need to convert one POJO into the following XML:

<Root>
  <Version>2.0</Version>
  <Name>John</Name>
  <Age>18</Age>
  <UserId>22491</UserId>
  <Country>USA</Country>
  <AnotherData>
    <Records>
      <AnotherRecord>
        <Field1>XXX</Field1>
        <Field2>XX</Field2>
        <Field3>CCCCCCCC</Field3>
        <Field4>XXX9000</Field4>
        <Field5>XXX00345</Field5>
      </AnotherRecord>
    </Records>
  </AnotherData>
</Root>

I know how to convert the fields below the root tag, it's not a problem. But from the AnotherData my problem's starting.

To represent the xml above I need some class like this:

puclic class Root{
    public String Version;
    public String Name;
    public String Age;
    public String UserID;
    public String Country;
    public AnotherData AnotherData;
}

public class AnotherData{
    public Records Records;
}

public class Records{
    List<AnotherRecord> list;
}

public class AnotherRecord{
    public String Field1;
    public String Field2;
    public String Field3;
    public String Field4;
    public String Field5;
}

But I don't need of this structure of class, I like implement my classes in a more simple mode, and "force" the tag structure in xml.

My class would be like below, but keeping the structure xml like above.

puclic class Root{
    public String Version;
    public String Name;
    public String Age;
    public String UserID;
    public String Country;
    public AnotherData AnotherData;
    List<AnotherRecord> list;
}

public class AnotherRecord{
    public String Field1;
    public String Field2;
    public String Field3;
    public String Field4;
    public String Field5;
}

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

I do not believe this use case can be handled by XStream. If you are open to using other technologies, below is an example of how it could be done with MOXy. Using the @XmlPath extension.

@XmlPath("AnotherData/Records/AnotherRecord")
List<AnotherRecord> list;

Root

Below is what the fully mapped Root class would look like. JAXB does not require any annotations (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html ), but since the XML elements in your document do not match the default naming rules some annotations are required.

package forum11970410;

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="Root")
public class Root{
    @XmlElement(name="Version")
    public String Version;

    @XmlElement(name="Name")
    public String Name;

    @XmlElement(name="Age")
    public String Age;

    @XmlElement(name="UserId")
    public String UserID;

    @XmlElement(name="Country")
    public String Country;

    @XmlPath("AnotherData/Records/AnotherRecord")
    List<AnotherRecord> list;
}

AnotherRecord

package forum11970410;

import javax.xml.bind.annotation.XmlElement;

public class AnotherRecord{
    @XmlElement(name="Field1")
    public String Field1;

    @XmlElement(name="Field2")
    public String Field2;

    @XmlElement(name="Field3")
    public String Field3;

    @XmlElement(name="Field4")
    public String Field4;

    @XmlElement(name="Field5")
    public String Field5;
}

jaxb.properties

To specify MOXy as your JAXB provider you need to add a file called jaxb.properties in the same package as your domain classes with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html ):

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

Demo

You can use the following demo code to prove that everything works:

package forum11970410;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11970410/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

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

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <Version>2.0</Version>
   <Name>John</Name>
   <Age>18</Age>
   <UserId>22491</UserId>
   <Country>USA</Country>
   <AnotherData>
      <Records>
         <AnotherRecord>
            <Field1>XXX</Field1>
            <Field2>XX</Field2>
            <Field3>CCCCCCCC</Field3>
            <Field4>XXX9000</Field4>
            <Field5>XXX00345</Field5>
         </AnotherRecord>
      </Records>
   </AnotherData>
</Root>

For More Information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM