我需要将一个 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>
我知道如何转换根标签下面的字段,这不是问题。但从 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;
}
最佳答案
注意:我是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;
}
另一条记录
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
要将 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
演示
您可以使用以下演示代码来证明一切正常:
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>
了解更多信息
关于java - 使用 Xstream 定义 XML 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11970410/