Need help in formatting JAXB output

后端 未结 2 1569
我在风中等你
我在风中等你 2021-01-13 16:54

I have some objects let\'s say two, A and B. These objects from the same class. I need to marshal these objects using JAXB and the output XML should be in this form:

2条回答
  •  没有蜡笔的小新
    2021-01-13 17:36

    package com.namasoft.dms.gui.common.utilities;
    
    import java.io.StringWriter;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.annotation.XmlAccessorOrder;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlID;
    import javax.xml.bind.annotation.XmlIDREF;
    import javax.xml.bind.annotation.XmlRootElement;
    
    public class JAXB
    {
    
        public static class Human
        {
    
            @XmlID
            String id;
            @XmlElement
            String name;
            @XmlElement
            int age;
    
            public Human()
            {
            }
    
            public Human(String name)
            {
                this.id = this.name = name;
                age = new Random().nextInt();
            }
        }
    
        @XmlRootElement
        public static class HumansList
        {
            @XmlElementWrapper(name = "humanObjects")
            @XmlElement(name = "human")
            List humanObjects = new ArrayList<>();
    
            @XmlIDREF
            @XmlElementWrapper(name = "humanIds")
            @XmlElement(name = "id")
            List humanIds = new ArrayList<>();
    
            void addHuman(Human human)
            {
                humanObjects.add(human);
                humanIds.add(human);
            }
        }
    
        public static void main(String[] args) throws JAXBException
        {
            HumansList list = new HumansList();
            Human parent1 = new Human("parent1");
            list.addHuman(parent1);
            Human child11 = new Human("child11");
            list.addHuman(child11);
            Human child12 = new Human("child12");
            list.addHuman(child12);
    
            Human parent2 = new Human("parent2");
            list.addHuman(parent2);
            Human child21 = new Human("child21");
            list.addHuman(child21);
            Human child22 = new Human("child22");
            list.addHuman(child22);
    
            JAXBContext context = JAXBContext.newInstance(HumansList.class);
            Marshaller m = context.createMarshaller();
            StringWriter xml = new StringWriter();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    
            m.marshal(list, xml);
            System.out.println(xml);
        }
    }
    

    The output will be

    
    
        
            
                parent1
                parent1
                -486071665
            
            
                child11
                child11
                920318383
            
            
                child12
                child12
                -1355111983
            
            
                parent2
                parent2
                -314154051
            
            
                child21
                child21
                983544381
            
            
                child22
                child22
                748252616
            
        
        
            parent1
            child11
            child12
            parent2
            child21
            child22
        
    
    

提交回复
热议问题