How to I output org.w3c.dom.Element to string format in java?

前端 未结 7 712
旧巷少年郎
旧巷少年郎 2020-11-27 13:01

I have an org.w3c.dom.Element object passed into my method. I need to see the whole xml string including its child nodes (the whole object graph). I am looking

7条回答
  •  我在风中等你
    2020-11-27 13:49

    If you have the schema of the XML or can otherwise create JAXB bindings for it, you could use the JAXB Marshaller to write to System.out:

    import javax.xml.bind.*;
    import javax.xml.bind.annotation.*;
    import javax.xml.namespace.QName;
    
    @XmlRootElement
    public class BoundClass {
    
        @XmlAttribute
        private String test;
    
        @XmlElement
        private int x;
    
        public BoundClass() {}
    
        public BoundClass(String test) {
            this.test = test;
        }
    
        public static void main(String[] args) throws Exception {
            JAXBContext jxbc = JAXBContext.newInstance(BoundClass.class);
            Marshaller marshaller = jxbc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.marshal(new JAXBElement(new QName("root"),BoundClass.class,new Main("test")),System.out);
        }
    }
    

提交回复
热议问题