I have this function in my code base, this should work for you.
public static Document loadXMLFromString(String xml) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
also see this similar question
Answer from shsteimer on Stack Overflow Top answer 1 of 7
523
I have this function in my code base, this should work for you.
public static Document loadXMLFromString(String xml) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
also see this similar question
2 of 7
20
One way is to use the version of parse that takes an InputSource rather than a file
A SAX InputSource can be constructed from a Reader object. One Reader object is the StringReader
So something like
parse(new InputSource(new StringReader(myString))) may work.
Videos
java sax parser xml string example
19:22
Handling XML Files in Java | DOM Parser Library | Parsing XML Files ...
Parse XML using the standard Java DOM parser
19:22
Handling XML Files in Java | DOM Parser Library | Parsing ...
11:46
Read XML tags in Java | XML DOM Parser | Java PDF tutorial | okay ...
Inductive Automation
docs.inductiveautomation.com › ignition platform › scripting › scripting examples › parsing xml with java libraries
Parsing XML with Java Libraries | Ignition User Manual
from javax.xml.parsers import DocumentBuilderFactory from java.io import ByteArrayInputStream # Define your XML string xmlString = """ <employee id="1234"> <name>John Smith</name> <start_date>2010-11-26</start_date> <department>IT</department> <title>Tech Support</title> </employee> """ # Replace with your actual XML string # Create a DOM document builder builderFactory = DocumentBuilderFactory.newInstance() builder = builderFactory.newDocumentBuilder() # Parse the XML string stream = ByteArrayInputStream(xmlString.encode('utf-8')) document = builder.parse(stream) # Access the root element root = document.getDocumentElement()
TutorialsPoint
tutorialspoint.com › java_xml › java_dom_parse_document.htm
Java DOM Parser - Parse XML Document
It returns the text content in the form of a String. Let us see the following example where we have one root element and a sub element. Here, 'college' is the root element with 'department' as sub element.
Java Code Geeks
javacodegeeks.com › home › core java
How to Parse XML from a String in Java - Java Code Geeks
November 18, 2024 - In this article, we explored two effective methods for parsing XML from a String in Java: using an InputSource with a StringReader and converting the String into an InputStream. Both approaches offer simple and efficient ways to work with XML ...
How to do in Java
howtodoinjava.com › home › java xml › java convert string to xml dom example
Java Convert String to XML DOM Example
September 3, 2023 - To get the XML dom from XML file, instead of passing the XML string to DocumentBuilder, pass the file path to let the parser read the file content directly. We have employees.xml file which has XML content, we will read to get XML document. <employees> <employee id="101"> <name>Lokesh Gupta</name> <title>Author</title> </employee> <employee id="102"> <name>Brian Lara</name> <title>Cricketer</title> </employee> </employees> import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class StringtoXMLExample
Top answer 1 of 6
82
Using JDOM:
String xml = "<message>HELLO!</message>";
org.jdom.input.SAXBuilder saxBuilder = new SAXBuilder();
try {
org.jdom.Document doc = saxBuilder.build(new StringReader(xml));
String message = doc.getRootElement().getText();
System.out.println(message);
} catch (JDOMException e) {
// handle JDOMException
} catch (IOException e) {
// handle IOException
}
Using the Xerces DOMParser:
String xml = "<message>HELLO!</message>";
DOMParser parser = new DOMParser();
try {
parser.parse(new InputSource(new java.io.StringReader(xml)));
Document doc = parser.getDocument();
String message = doc.getDocumentElement().getTextContent();
System.out.println(message);
} catch (SAXException e) {
// handle SAXException
} catch (IOException e) {
// handle IOException
}
Using the JAXP interfaces:
String xml = "<message>HELLO!</message>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
try {
Document doc = db.parse(is);
String message = doc.getDocumentElement().getTextContent();
System.out.println(message);
} catch (SAXException e) {
// handle SAXException
} catch (IOException e) {
// handle IOException
}
} catch (ParserConfigurationException e1) {
// handle ParserConfigurationException
}
2 of 6
10
You could also use tools provided by the base JRE:
String msg = "<message>HELLO!</message>";
DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document parse = newDocumentBuilder.parse(new ByteArrayInputStream(msg.getBytes()));
System.out.println(parse.getFirstChild().getTextContent());
Java2s
java2s.com › Code › Java › XML › ParseanXMLstringUsingDOMandaStringReader.htm
Parse an XML string: Using DOM and a StringReader. : DOM « XML « Java
Parse an XML string: Using DOM and a StringReader. : DOM « XML « Java
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 - <?xml version="1.0" encoding="UTF-8"?><Emp id="1"><name>Pankaj</name><age>25</age> <role>Developer</role><gen>Male</gen></Emp> You can use replaceAll("\n|\r", "") to remove new line characters from String and get it in compact format. Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. ... Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean).
Mkyong
mkyong.com › home › java › java – convert string to xml
Java - Convert String to XML - Mkyong.com
July 29, 2022 - This example shows how to use a JDOM2 parser to convert a String to an XML document and back to a String. ... <dependency> <groupId>org.jdom</groupId> <artifactId>jdom2</artifactId> <version>2.0.6</version> </dependency> ... package com.mkyong.xml.tips; import org.jdom2.Document; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; import javax.xml.XMLConstants; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; // JDOM2 Parser public class ConvertStringToXmlJDom2 { final stat
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
Oracle
docs.oracle.com › en › database › oracle › oracle-database › 21 › adxdk › XML-parsing-for-Java.html
12 XML Parsing for Java
To write a program that lets a ... To insert this information into an XML document, you can use either of these techniques: Create an XML document in a string and then parse it....
Real's HowTo
rgagnon.com › javadetails › java-0573.html
Parse an XML string - Real's Java How-to
Got it Using DOM and a StringReader. import javax.xml.parsers.*; import org.xml.sax.InputSource; import org.w3c.dom.*; import java.io.*; public class ParseXMLString { public static void main(String arg[]) { String xmlRecords = "<data>" + " <employee>" + " <name>John</name>" + " <title>Manager</title>" + " </employee>" + " <employee>" + " <name>Sara</name>" + " <title>Clerk</title>" + " </employee>" + "</data>"; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new Str
DigitalOcean
digitalocean.com › community › tutorials › java-xml-parser
Java XML Parser | DigitalOcean
August 4, 2022 - I required object type SOAP parsing please provide me small information so i will do that. ... Hi , valuable info sir … I need to read coordinates (x,y) from xml and show it in image form (graph) using java . Can you suggest me how? ... Nice Tutorial Is java providing any API to generate XSD programmatically. I having below information using that i want to generate XSD. Root Tag = item | FieldName | FieldType | Xpath | | name | string | “/item” | | quantity | Integer | “/item” | | price | price | “/item” | | amount | Integer | “/item/price” | | currency | string | “/item/price” | My output XSD should look like as below
Tutorialspoint
tutorialspoint.com › java › xml › javax_xml_parsers_documentbuilder_parse_string.htm
Javax.xml.parsers.DocumentBuilder.parse() Method
The Javax.xml.parsers.DocumentBuilder.parse(String uri) method parses the content of the given URI as an XML document and return a new DOM Document object.
Oracle
docs.oracle.com › cd › E14571_01 › appdev.1111 › b28394 › adx_j_parser.htm
4 XML Parsing for Java
This code fragment from DOMSample.java shows how to parse an instance of the java.net.URL class: ... Note that the XML input can be a file, string buffer, or URL.
Codoid
codoid.com › automation-testing › read-data-from-xml-by-using-different-parsers-in-java
Read Data from XML by Using Different Parsers in Java - Codoid
September 18, 2024 - The Xpath parser is a query language that is used to find the node from an XML file and parse the XML based on the query string.
Call 1800 (212) 6988
Address TIDEL Park, 305, 3rd Floor, D-North, 4, Rajiv Gandhi Salai, Tharamani,, 600113, Chennai