将复杂的 XML 转换为 Java 对象

我有一个 xml,我想将由特定标签的子级形成的子 xml 保存到一个字符串中。这是一个 xml 示例:


    <?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>

我想通过例如“someList”将子xml元素和值保存到字符串中,因为接下来我将它反序列化为java对象


慕侠2389804
浏览 404回答 3
3回答

喵喵时光机

使用 JAXB 解组器将 xml 文档转换为 java 对象。首先将 JAXB 依赖项添加到项目的类路径中。了解更多信息SampleDTO.java@XmlRootElementpublic class SampleDTO {&nbsp; &nbsp; private String id;&nbsp; &nbsp; private List<SomeList> someList;&nbsp; &nbsp; private List<OtherList> otherList;&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public List<SomeList> getSomeList() {&nbsp; &nbsp; &nbsp; &nbsp; return someList;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSomeList(List<SomeList> someList) {&nbsp; &nbsp; &nbsp; &nbsp; this.someList = someList;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public List<OtherList> getOtherList() {&nbsp; &nbsp; &nbsp; &nbsp; return otherList;&nbsp; &nbsp; }&nbsp; &nbsp; public void setOtherList(List<OtherList> otherList) {&nbsp; &nbsp; &nbsp; &nbsp; this.otherList = otherList;&nbsp; &nbsp; }}SomeList.java@XmlRootElementpublic class SomeList {&nbsp; &nbsp; private List<SomeObject> someObject;&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public List<SomeObject> getSomeObject() {&nbsp; &nbsp; &nbsp; &nbsp; return someObject;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSomeObject(List<SomeObject> someObject) {&nbsp; &nbsp; &nbsp; &nbsp; this.someObject = someObject;&nbsp; &nbsp; }}OtherList.java@XmlRootElementpublic class OtherList {&nbsp; &nbsp; private List<OtherObject> otherObject;&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public List<OtherObject> getOtherObject() {&nbsp; &nbsp; &nbsp; &nbsp; return otherObject;&nbsp; &nbsp; }&nbsp; &nbsp; public void setOtherObject(List<OtherObject> otherObject) {&nbsp; &nbsp; &nbsp; &nbsp; this.otherObject = otherObject;&nbsp; &nbsp; }}SomeObject.java@XmlRootElementpublic class SomeObject {&nbsp; &nbsp; private String amount;&nbsp; &nbsp; private String id;&nbsp; &nbsp; private String someDescription;&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String getAmount() {&nbsp; &nbsp; &nbsp; &nbsp; return amount;&nbsp; &nbsp; }&nbsp; &nbsp; public void setAmount(String amount) {&nbsp; &nbsp; &nbsp; &nbsp; this.amount = amount;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String getSomeDescription() {&nbsp; &nbsp; &nbsp; &nbsp; return someDescription;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSomeDescription(String someDescription) {&nbsp; &nbsp; &nbsp; &nbsp; this.someDescription = someDescription;&nbsp; &nbsp; }}OtherObject.java@XmlRootElementpublic class OtherObject {&nbsp; &nbsp; private String flag;&nbsp; &nbsp; private String id;&nbsp; &nbsp; private String otherDescription;&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String getFlag() {&nbsp; &nbsp; &nbsp; &nbsp; return flag;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFlag(String flag) {&nbsp; &nbsp; &nbsp; &nbsp; this.flag = flag;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String getOtherDescription() {&nbsp; &nbsp; &nbsp; &nbsp; return otherDescription;&nbsp; &nbsp; }&nbsp; &nbsp; public void setOtherDescription(String otherDescription) {&nbsp; &nbsp; &nbsp; &nbsp; this.otherDescription = otherDescription;&nbsp; &nbsp; }}使用 JAXB 解组public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; File file = new File("file.xml");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; JAXBContext jaxbContext = JAXBContext.newInstance(SampleDTO.class);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; SampleDTO sampleDTO= (SampleDTO) jaxbUnmarshaller.unmarshal(file);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; } catch (JAXBException e) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp;}

慕村225694

有许多可用的开源 XML 处理包。我喜欢杰克逊。这是关于 Jackson XML 的 Baeldung 文章的链接总结是这样的:将 Jackson 依赖项添加到您的 POM。创建一个代表您的 xml 结构的对象结构。创建一个 XmlMapper。使用 XmlMapper。

慕标5832272

你的 java 类/对象应该至少有这 3 个实例变量:private int amountprivate int idprivate String description然后使用一些 xml 解析库(例如jdom2),并且对于您迭代的每个<someObject>标签,初始化您的类的一个新对象并为其分配从 xml 解析的值(数量/id/描述),并添加每个新创建的对象在列表或数组等中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java