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

I have an org.w3c.dom.Element object passed to my method. I need to see the entire xml string, including its child nodes (the entire graphic). I'm looking for a method that can convert Element to an XML format string that I can include System.out.println . Just println() in the Element object will not work, because toString() will not output the xml format and will not go through its child element node. Is there an easy way without writing my own method for this? Thank.

+79
java dom xml
Aug 02 '09 at 19:21
source share
7 answers

Assuming you want to stick with the standard API ...

You can use DOMImplementationLS :

 Document document = node.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document .getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); String str = serializer.writeToString(node); 

If the value <? xml version = "1.0" encoding = "UTF-16"? > the ad is bothering you, you can use transformer instead:

 TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); String str = buffer.toString(); 
+144
Aug 02 '09 at 20:47
source share

Simple 4-line code to get String without xml declaration ( <?xml version="1.0" encoding="UTF-16"?> ) From org.w3c.dom.Element

 DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0"); LSSerializer serializer = lsImpl.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", false); //by default its true, so set it to false to get String without xml-declaration String str = serializer.writeToString(node); 
+11
Oct 31 '13 at 8:38
source share

If you have an XML schema or you can create JAXB bindings for it otherwise, you can use Marshaller JAXB 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); } } 
+2
Aug 02 '09 at 20:49
source share

It is not supported in the standard JAXP API, for this purpose I used the JDom library. It has a printer function, formatting options, etc. http://www.jdom.org/

+1
Aug 02 '09 at 20:02
source share

Try jcabi-xml with one layer:

 String xml = new XMLDocument(element).toString(); 
0
Apr 04 '14 at 8:02
source share

With VTD-XML, you can jump to the cursor and make one call to getElementFragment to retrieve a segment (as indicated by its offset and length) ... Below is an example

 import com.ximpleware.*; public class concatTest{ public static void main(String s1[]) throws Exception { VTDGen vg= new VTDGen(); String s = "<users><user><firstName>some </firstName><lastName> one</lastName></user></users>"; vg.setDoc(s.getBytes()); vg.parse(false); VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath("/users/user/firstName"); int i=ap.evalXPath(); if (i!=1){ long l= vn.getElementFragment(); System.out.println(" the segment is "+ vn.toString((int)l,(int)(l>>32))); } } } 
0
Apr 02 '16 at 0:41
source share

this is what i did in jcabi:

 private String asString(Node node) { StringWriter writer = new StringWriter(); try { Transformer trans = TransformerFactory.newInstance().newTransformer(); // @checkstyle MultipleStringLiterals (1 line) trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(OutputKeys.VERSION, "1.0"); if (!(node instanceof Document)) { trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } trans.transform(new DOMSource(node), new StreamResult(writer)); } catch (final TransformerConfigurationException ex) { throw new IllegalStateException(ex); } catch (final TransformerException ex) { throw new IllegalArgumentException(ex); } return writer.toString(); } 

and it works for me!

0
Mar 21 '17 at 9:15
source share



All Articles