[java] XML Document to String

What's the simplest way to get the String representation of a XML Document (org.w3c.dom.Document)? That is all nodes will be on a single line.

As an example, from

<root>
  <a>trge</a>
  <b>156</b>
</root>

(this is only a tree representation, in my code it's a org.w3c.dom.Document object, so I can't treat it as a String)

to

"<root> <a>trge</a> <b>156</b> </root>"

Thanks!

This question is related to java xml string dom xmldocument

The answer is


First you need to get rid of all newline characters in all your text nodes. Then you can use an identity transform to output your DOM tree. Look at the javadoc for TransformerFactory#newTransformer().


Use the Apache XMLSerializer

here's an example: http://www.informit.com/articles/article.asp?p=31349&seqNum=3&rl=1

you can check this as well

http://www.netomatix.com/XmlFileToString.aspx


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to dom

How do you set the document title in React? How to find if element with specific id exists or not Cannot read property 'style' of undefined -- Uncaught Type Error adding text to an existing text element in javascript via DOM Violation Long running JavaScript task took xx ms How to get `DOM Element` in Angular 2? Angular2, what is the correct way to disable an anchor element? React.js: How to append a component on click? Detect click outside React component DOM element to corresponding vue.js component

Examples related to xmldocument

Why "Data at the root level is invalid. Line 1, position 1." for XML Document? Read XML file into XmlDocument Getting specified Node values from XML document XML Document to String How to modify existing XML file with XmlDocument and XmlNode in C# Convert XmlDocument to String XDocument or XmlDocument Read XML Attribute using XmlDocument How to change XML Attribute What is the simplest way to get indented XML with line breaks from XmlDocument?