Created
June 22, 2024 10:06
-
-
Save vernetto/9b0d79bdf41f273c194a0966428e5483 to your computer and use it in GitHub Desktop.
xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To convert a org.w3c.dom.Document to an XML string in Java, you can use the Transformer class provided by the javax.xml.transform package. Here’s a step-by-step guide to achieving this: | |
Import the necessary classes: | |
Ensure you have the required imports for the transformation. | |
java | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import org.w3c.dom.Document; | |
import java.io.StringWriter; | |
Create a method to perform the conversion: | |
This method takes a Document object and returns the XML string. | |
java | |
public class XMLConverter { | |
public static String convertDocumentToString(Document doc) { | |
try { | |
// Set up a transformer | |
TransformerFactory tf = TransformerFactory.newInstance(); | |
Transformer transformer = tf.newTransformer(); | |
// Set up the output properties for the transformer | |
transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "no"); | |
transformer.setOutputProperty(javax.xml.transform.OutputKeys.METHOD, "xml"); | |
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); | |
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "UTF-8"); | |
// Convert the Document to a string | |
StringWriter writer = new StringWriter(); | |
transformer.transform(new DOMSource(doc), new StreamResult(writer)); | |
return writer.getBuffer().toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} | |
Use the method in your code: | |
You can now use this method to convert any Document object to its XML string representation. | |
java | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import org.w3c.dom.Document; | |
public class Main { | |
public static void main(String[] args) { | |
try { | |
// Sample code to create a Document (for demonstration) | |
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
Document doc = dBuilder.newDocument(); | |
// Adding sample elements to the Document | |
org.w3c.dom.Element rootElement = doc.createElement("root"); | |
doc.appendChild(rootElement); | |
org.w3c.dom.Element childElement = doc.createElement("child"); | |
childElement.appendChild(doc.createTextNode("Sample Text")); | |
rootElement.appendChild(childElement); | |
// Convert Document to XML String | |
String xmlString = XMLConverter.convertDocumentToString(doc); | |
System.out.println(xmlString); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
This code will convert a Document to a formatted XML string with proper indentation and encoding. Adjust the output properties as needed based on your requirements. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment