DigitalOcean
digitalocean.com › community › tutorials › java-convert-string-to-xml-document-and-xml-document-to-string
Java Convert String to XML Document and XML Document to String | DigitalOcean
August 3, 2022 - Sometimes while programming in java, we get String which is actually an XML and to process it, we need to convert it to XML Document (org.w3c.dom.Document). Also for debugging purpose or to send to some other function, we might need to convert Document object to String.
Videos
12:16
How to Read XML File in Java (Step-by-Step for Beginners) - YouTube
14:26
Read XML file in Java | process XML file in java | XML DOM Parser ...
19:22
Handling XML Files in Java | DOM Parser Library | Parsing XML Files ...
19:19
How to read data from the XML files in Java (Core Java Interview ...
XML en Java, Documentos, elementos/Nodos, XPath
TutorialsPoint
tutorialspoint.com › java_xml › java_dom_create_document.htm
Java DOM Parser - Create XML Document
Java DOM parser API has methods, interfaces and classes to create XML documents. Using this API, we can create XML documents from the scratch through our Java applications. The createElement() method creates new elements and the appendChild() method
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.xml › module-summary.html
java.xml (Java SE 11 & JDK 11 )
January 20, 2026 - Defines the Java API for XML Processing (JAXP), the Streaming API for XML (StAX), the Simple API for XML (SAX), and the W3C Document Object Model (DOM) API.
Oracle
docs.oracle.com › javase › 8 › docs › api › org › w3c › dom › Document.html
Document (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... The Document interface represents the entire HTML or XML document.
Mkyong
mkyong.com › home › java › how to write xml file in java – (dom parser)
How to write XML file in Java – (DOM Parser) - Mkyong.com
May 12, 2021 - This tutorial shows how to use the Java built-in DOM APIs to write data to an XML file. ... Steps to create and write XML to a file. Create a Document doc.
Vogella
vogella.com › tutorials › JavaXML › article.html
Java and XML - Tutorial
November 11, 2025 - Java Architecture for XML Binding (JAXB) is a Java standard that allows to convert Java objects to XML and vice versa. JAXB defines a programmer API for reading and writing Java objects to from XML documents. It also defines a service provider that allows the selection of the JAXB implementation.
ZetCode
zetcode.com › java › dom
Java DOM - read and write XML with DOM in Java
January 27, 2024 - Java DOM tutorial shows how to use Java DOM API to read and write XML documents.
TutorialsPoint
tutorialspoint.com › java_xml › java_dom_parse_document.htm
Java DOM Parser - Parse XML Document
Java DOM parser is a Java API to parse any XML document. Using the methods provided, we can retrieve root element, sub elements and their attributes using Java DOM parser. In this tutorial we have used the getTagName() method to retrieve the tag
Oracle
docs.oracle.com › javase › tutorial › jaxp › dom › readingXML.html
Reading XML Data into a DOM (The Java™ Tutorials > Java API for XML Processing (JAXP) > Document Object Model)
This JAXP Java tutorial describes Java API for XML Processing (jaxp), XSLT, SAX, and related XML topics
Reintech
reintech.io › blog › java-xml-parsing-reading-and-manipulating-xml-documents
Java XML parsing: Reading and manipulating XML documents | Reintech media
April 18, 2023 - The DOM parser loads the entire XML document into memory and represents it as a tree structure. This allows you to easily traverse and manipulate the XML data. The main downside of the DOM parser is that it may consume a lot of memory for large XML files. To use the DOM parser, you need to import the following packages: import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList;
Top answer 1 of 10
58
There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester.
You could always use the standard JDK method of getting a document :
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
[...]
File file = new File("some/path");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
2 of 10
40
XML Code:
<?xml version="1.0"?>
<company>
<staff id="1001">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
Java Code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("/Users/mkyong/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : "
+ eElement.getAttribute("id"));
System.out.println("First Name : "
+ eElement.getElementsByTagName("firstname")
.item(0).getTextContent());
System.out.println("Last Name : "
+ eElement.getElementsByTagName("lastname")
.item(0).getTextContent());
System.out.println("Nick Name : "
+ eElement.getElementsByTagName("nickname")
.item(0).getTextContent());
System.out.println("Salary : "
+ eElement.getElementsByTagName("salary")
.item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
----------------
Root element :company
----------------------------
Current Element :staff
Staff id : 1001
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000
Current Element :staff
Staff id : 2001
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000
I recommended you reading this: Normalization in DOM parsing with java - how does it work?
Example source.
Oracle
docs.oracle.com › javase › tutorial › jaxp › xslt › writingDom.html
Writing Out a DOM as an XML File (The Java™ Tutorials > Java API for XML Processing (JAXP) > Extensible Stylesheet Language Transformations)
The code discussed in this section is in TranformationApp02.java. If you have not done so already, download the XSLT examples and unzip them into the install-dir/jaxp-1_4_2-release-date/samples directory. The only difference in the process is that now you will create a DOMSource using a node in the DOM, rather than the entire DOM. The first step is to import the classes you need to get the node you want, as shown in the following highlighted code: import org.w3c.dom.Document; import org.w3c.dom.DOMException; import org.w3c.dom.Node; import org.w3c.dom.NodeList;
Java
download.java.net › java › early_access › loom › docs › api › java.xml › javax › xml › package-summary.html
javax.xml (Java SE 25 & JDK 25 [build 1])
Defines XML/Java Type Mappings. ... Defines XML Namespace processing. ... Provides the classes for processing XML documents with a SAX (Simple API for XML) parser or a DOM (Document Object Model) Document builder.
Mkyong
mkyong.com › home › java › how to read xml file in java – (dom parser)
How to read XML file in Java - (DOM Parser) - Mkyong.com
April 1, 2021 - package com.mkyong.xml.dom; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.io.InputStream; public class ReadXmlDomParser { private static final String FILENAME = "/users/mkyong/staff.xml"; public static void main(String[] args) { // Instantiate the Factory DocumentBuilderFactory dbf = DocumentBu