将递归 XML 转换为 POJO 并返回
Convert recursive XML to POJO and back
我XML
是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Trees>
<Tree Id="1" Name="FirstTree" Type="Main Tree">
<Description>Main Tree</Description>
<Versions >
<Version Id="20592" RootNodeId="117341" CreateDate="2018-01-17 17:01:38 Europe/Moscow" Status="EDIT" Label="TestTree">
<Switch Id="117341" DisplayName="root structure"/>
<Switch Id="117342" DisplayName="root structure">
<ScalarCase Id="40808"/>
<Switch Id="117343" DisplayName="root structure">
<ScalarCase Id="40809"/>
<Switch Id="117344" DisplayName="root structure">
<ScalarCase Id="40810"/>
<Leaf Id="117345"/>
<Condition Id="117346">
<Leaf Id="117347"/>
</Condition>
</Switch>
</Switch>
</Switch>
</<Version>
</Versions>
</Tree>
</Trees>
我的 POJO
怎么会像这个 XML
的结构?尚不清楚 POJO 应该如何描述 Version 对象。我有抽象 class Node
并创建了 3 个继承者:Switch
、Leaf
和 Condition
.
如何递归嵌套此类对象以将 XML
转换为对象并返回?
假设您已经创建了反映您的 xml 的 POJO,从:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Trees")
public class Trees {
private Tree Tree;
// getters and setters
}
切换到class(其他的我这里就不写了):
@XmlAccessorType(XmlAccessType.FIELD)
public class Switch {
private Condition Condition;
private ScalarCase ScalarCase;
private String Id;
private String DisplayName;
private Leaf Leaf;
@XmlElement(name="Switch")
private Switch aSwitch;
// getters and setters
}
你所有的 POJO 都有正确的注释等。
然后尝试将 xml 读入 POJO,似乎有效:
public static void main(String[] args) {
try {
File file = new File("trees.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Trees.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Trees trees = (Trees) jaxbUnmarshaller.unmarshal(file);
System.out.println(trees);
} catch (JAXBException e) {
e.printStackTrace();
}
}
编辑:
回复评论区的评论也一样。
当 class 包含一个以自身为类型的字段时,它实质上会创建一个 LinkedList,因此您可以拥有任何想要的深度(在内存限制内)。
我XML
是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Trees>
<Tree Id="1" Name="FirstTree" Type="Main Tree">
<Description>Main Tree</Description>
<Versions >
<Version Id="20592" RootNodeId="117341" CreateDate="2018-01-17 17:01:38 Europe/Moscow" Status="EDIT" Label="TestTree">
<Switch Id="117341" DisplayName="root structure"/>
<Switch Id="117342" DisplayName="root structure">
<ScalarCase Id="40808"/>
<Switch Id="117343" DisplayName="root structure">
<ScalarCase Id="40809"/>
<Switch Id="117344" DisplayName="root structure">
<ScalarCase Id="40810"/>
<Leaf Id="117345"/>
<Condition Id="117346">
<Leaf Id="117347"/>
</Condition>
</Switch>
</Switch>
</Switch>
</<Version>
</Versions>
</Tree>
</Trees>
我的 POJO
怎么会像这个 XML
的结构?尚不清楚 POJO 应该如何描述 Version 对象。我有抽象 class Node
并创建了 3 个继承者:Switch
、Leaf
和 Condition
.
如何递归嵌套此类对象以将 XML
转换为对象并返回?
假设您已经创建了反映您的 xml 的 POJO,从:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Trees")
public class Trees {
private Tree Tree;
// getters and setters
}
切换到class(其他的我这里就不写了):
@XmlAccessorType(XmlAccessType.FIELD)
public class Switch {
private Condition Condition;
private ScalarCase ScalarCase;
private String Id;
private String DisplayName;
private Leaf Leaf;
@XmlElement(name="Switch")
private Switch aSwitch;
// getters and setters
}
你所有的 POJO 都有正确的注释等。
然后尝试将 xml 读入 POJO,似乎有效:
public static void main(String[] args) {
try {
File file = new File("trees.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Trees.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Trees trees = (Trees) jaxbUnmarshaller.unmarshal(file);
System.out.println(trees);
} catch (JAXBException e) {
e.printStackTrace();
}
}
编辑: 回复评论区的评论也一样。
当 class 包含一个以自身为类型的字段时,它实质上会创建一个 LinkedList,因此您可以拥有任何想要的深度(在内存限制内)。