将复杂XML转换为Java对象
我有一个xml,我想将由特定标记的子项形成的子xml保存到一个字符串中。 这是一个xml示例:将复杂XML转换为Java对象,java,xml,deserialization,Java,Xml,Deserialization,我有一个xml,我想将由特定标记的子项形成的子xml保存到一个字符串中。 这是一个xml示例: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <SampleDTO> <id>1</id> <someList> <someObject> <amount>32</amount>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SampleDTO>
<id>1</id>
<someList>
<someObject>
<amount>32</amount>
<id>1</id>
<someDescription>I am a description</someDescription>
</someObject>
<someObject>
<amount>66</amount>
<id>2</id>
<someDescription>I am another description</someDescription>
</someObject>
<someObject>
<amount>78</amount>
<id>13</id>
<someDescription>Guess what? I am a description</someDescription>
</someObject>
</someList>
<otherList>
<otherObject>
<flag>true</flag>
<id>1</id>
<otherDescription>Oh nice, a description</otherDescription>
</otherObject>
</otherList>
</SampleDTO>
1.
32
1.
我是一个描述
66
2.
我是另一个描述
78
13
你猜怎么着?我是一个描述
真的
1.
哦,很好,描述
例如,我希望通过传递“someList”将子xml元素和值保存到字符串中,因为接下来我将其反序列化为java对象您的java类/对象应至少具有以下3个实例变量:
private int amount
private int id
private String description
然后使用一些xml解析库(eg),对于您迭代的每个
标记,初始化类的新对象,并将从xml解析的值(amount/id/description)分配给它,然后将每个新创建的对象添加到列表或数组等中。有许多开源xml处理包可用。
我喜欢杰克逊。
这里是一个链接到
总结如下:
使用JAXB解组器将xml文档转换为java对象。 首先,将JAXB依赖项添加到项目的类路径中 SampleDTO.java
@XmlRootElement
public class SampleDTO {
private String id;
private List<SomeList> someList;
private List<OtherList> otherList;
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public List<SomeList> getSomeList() {
return someList;
}
public void setSomeList(List<SomeList> someList) {
this.someList = someList;
}
@XmlElement
public List<OtherList> getOtherList() {
return otherList;
}
public void setOtherList(List<OtherList> otherList) {
this.otherList = otherList;
}
}
OtherObject.java
@XmlRootElement
public class SomeObject {
private String amount;
private String id;
private String someDescription;
@XmlElement
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getSomeDescription() {
return someDescription;
}
public void setSomeDescription(String someDescription) {
this.someDescription = someDescription;
}
}
@XmlRootElement
public class OtherObject {
private String flag;
private String id;
private String otherDescription;
@XmlElement
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getOtherDescription() {
return otherDescription;
}
public void setOtherDescription(String otherDescription) {
this.otherDescription = otherDescription;
}
}
用JAXB解组
public class Main {
public static void main(String[] args) {
try {
File file = new File("file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(SampleDTO.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
SampleDTO sampleDTO= (SampleDTO) jaxbUnmarshaller.unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
对不起,我重新编辑了,我看一下。我认为您需要一个XPath表达式来获取正确的子树,然后对其进行反序列化。如果你真的需要把它变成java对象。也许将其作为XML树读取和解析更容易。
@XmlRootElement
public class OtherObject {
private String flag;
private String id;
private String otherDescription;
@XmlElement
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getOtherDescription() {
return otherDescription;
}
public void setOtherDescription(String otherDescription) {
this.otherDescription = otherDescription;
}
}
public class Main {
public static void main(String[] args) {
try {
File file = new File("file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(SampleDTO.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
SampleDTO sampleDTO= (SampleDTO) jaxbUnmarshaller.unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}