简体   繁体   English

使用Xstream定义XML结构

[英]Define XML Structure with Xstream

I need to convert one POJO into the following XML: 我需要将一个POJO转换为以下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. 但是从AnotherData开始,我的问题开始了。

To represent the xml above I need some class like this: 为了表示上面的xml,我需要这样的类:

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. 但是我不需要这种类结构,我喜欢以更简单的方式实现我的类,并在xml中“强制”标记结构。

My class would be like below, but keeping the structure xml like above. 我的类如下所示,但保持结构xml类似于以上。

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. 注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB(JSR-222)专家组的成员。

I do not believe this use case can be handled by XStream. 我不认为XStream可以处理这种用例。 If you are open to using other technologies, below is an example of how it could be done with MOXy. 如果您愿意使用其他技术,则下面是使用MOXy的示例。 Using the @XmlPath extension. 使用@XmlPath扩展名。

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

Root

Below is what the fully mapped Root class would look like. 下面是完全映射的Root类的外观。 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. JAXB不需要任何注释(请参阅: http : //blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html ),但是由于文档中的XML元素与默认的命名规则不匹配,注释是必需的。

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 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 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 ): 要将MOXy指定为JAXB提供程序,您需要在与域类相同的包中添加一个名为jaxb.properties的文件,并带有以下条目(请参阅: 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 input.xml /输出

<?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 欲获得更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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