Mapping XML Entities to Java Objects

前端 未结 2 1334
傲寒
傲寒 2020-12-16 11:17

I am quite sure, this is one of the many duplicated questions around XML to Java Object conversions. But I started this thread since I couldn\'t find simpler or looking for

相关标签:
2条回答
  • 2020-12-16 12:11

    Basically you want to unmarshal your XML. Here's a detailed tutorial that describes how to use the JAXB xjc command to generate a Java class from XML Schema. A maven xjc plugin is also available for your convenience.

    0 讨论(0)
  • 2020-12-16 12:19

    Below is how you could map your object to XML using JAXB (JSR-222). An implementation is included in the JDK/JRE starting with Java SE 6. JAXB is supported by Spring (see section 8.5: http://static.springsource.org/spring-ws/site/reference/html/oxm.html)

    SummaryCart

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="SummaryCart", namespace="SummaryCart")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class SummaryCart{
    
        @XmlElement(name="SummaryElement")
        private List<SummaryElement> summaryElementList;
    
    }
    

    SummaryElement

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class SummaryElement {
    
        private int order;
        private String id;
        private String displayName;
        private String property;
        private List<SummaryElement> subElements;
        private int maxlines;
    
        @XmlAttribute
        private String type;
    
    }
    

    Demo

    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(SummaryCart.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum15881876/input.xml");
            SummaryCart sc = (SummaryCart) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "SummaryCart.xsd");
            marshaller.marshal(sc, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ns2:SummaryCart xmlns:ns2="SummaryCart" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="SummaryCart.xsd">
        <SummaryElement type="test">
            <order>1</order>
            <id>A</id>
            <displayName>A</displayName>
            <subElements>
                <order>1</order>
                <id>Preactivation</id>
                <displayName>Preactivation</displayName>
                <maxlines>0</maxlines>
            </subElements>
            <maxlines>1</maxlines>
        </SummaryElement>
    </ns2:SummaryCart>
    
    0 讨论(0)
提交回复
热议问题