2012-08-15 52 views
1

我需要一个POJO转换成以下XML:定义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> 

我知道如何将字段转换根标签的下方,这不是一个问题。但从AnotherData我的问题的开始。

为了表示上面的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; 
} 

但我不需要类的这个结构,我想实现一个更简单的模式我的课,而“力” XML中的标签结构。

我的类将如下所示,但保持像上面那样的结构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; 
} 
+0

什么你已经使用XStream试试?我不明白你确切的问题是什么。 – migu 2012-08-15 13:47:48

+0

此用例可以使用EclipseLink JAXB(MOXy)的“@ XmlPath”扩展名轻松映射(请参阅:http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html( 。请让我知道如果你有兴趣在演示如何完成这个例子。 – 2012-08-15 15:00:02

+0

使用另一个XML库是一个选项吗?我会推荐[SimpleXML](http://simple.sourceforge.net/)。它更简单比XStream更容易。 – davidbuzatto 2012-08-19 15:53:20

回答

1

注:我是EclipseLink JAXB (MOXy)铅和JAXB (JSR-222)专家小组的成员。

我不认为这个用例可以被XStream处理。如果您愿意使用其他技术,下面是如何使用MOXY完成的一个例子。使用@XmlPath扩展名。

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

下面是完全映射Root类会是什么样子。 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

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提供者,你需要在同一个包添加一个名为jaxb.properties文件作为您的域名班,以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

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

演示

您可以使用下面的演示代码来证明一切正常:

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

} 

输入。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> 

更多信息