In Java, How to Parse Xml as a String Instead of a File

In Java, how do I parse XML as a String instead of a file?

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

Best way to parse an XML String in Java?

To answer your question directly - to my knowledge, there is not a better way. The input source is used because it is more universal and can handle input from a file, a String or across the wire is my understanding.

You could also try using the SAX Xml parser - it is a little more basic, and uses the Visitor Pattern, but it gets the job done and for smallish data sets and simple XML schemas it is pretty easy to use. SAX is also included with the core JRE.

Convert XML-File to string without manipulation or optimization in Java

Firstly, as others have stated, you shouldn't use any XML processing. Just read the file as a text file.

Secondly, your umlaut characters showing up as '�' is due to an incorrect charset (encoding) being used. The charset error may be in your code, or it may be the XML file.

The original XML file contains encoding="windows-1252", but it's unusual for XML to be encoded in anything other than UTF-8, so I suspect the file is really a UTF-8 file and the encoding it claims to use is not correct.

Try forcing UTF-8 when reading the file. It's good practice, regardless, to specify the charset when converting bytes to text:

String xml = new String(
Files.readAllBytes(xmlFile.toPath()), StandardCharsets.UTF_8);

How to read XML content as String from a file in Java

One way to do this is to use Streaming feature of JaxB which effectivly uses SAX underneath. Here is an example:

 // create JAXBContext for the primer.xsd
JAXBContext context = JAXBContext.newInstance("primer");

Unmarshaller unmarshaller = context.createUnmarshaller();

// purchase order notification callback
final PurchaseOrders.Listener orderListener = new PurchaseOrders.Listener() {
public void handlePurchaseOrder(PurchaseOrders purchaseOrders, PurchaseOrderType purchaseOrder) {
System.out.println("this order will be shipped to "
+ purchaseOrder.getShipTo().getName());
}
};

// install the callback on all PurchaseOrders instances
unmarshaller.setListener(new Unmarshaller.Listener() {
public void beforeUnmarshal(Object target, Object parent) {
if(target instanceof PurchaseOrders) {
((PurchaseOrders)target).setPurchaseOrderListener(orderListener);
}
}

public void afterUnmarshal(Object target, Object parent) {
if(target instanceof PurchaseOrders) {
((PurchaseOrders)target).setPurchaseOrderListener(null);
}
}
});

// create a new XML parser
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader reader = factory.newSAXParser().getXMLReader();
reader.setContentHandler(unmarshaller.getUnmarshallerHandler());

for (String arg : args) {
// parse all the documents specified via the command line.
// note that XMLReader expects an URL, not a file name.
// so we need conversion.
reader.parse(new File(arg).toURI().toString());
}
}
}

It is taken straight from jaxB samples in jaxb/ri
https://github.com/javaee/jaxb-v2/blob/master/jaxb-ri/samples/src/main/samples/streaming-unmarshalling/src/Main.java

The PurchaseOrders.Listener interface is :

public static interface Listener {
void handlePurchaseOrder(PurchaseOrders purchaseOrders, PurchaseOrderType purchaseOrder);
}

How to parse a String containing XML in Java and retrieve the value of the root node?

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
}

In Java, how do I parse XML as a String instead of a file?

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

Save XML format to a string instead of a file

This example may help you out.

import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DOMBasicDoc {
public static void main(String args[]) {
try {
String[] input = {"John Doe,123-456-7890", "Bob Smith,123-555-1212"};
String[] line = new String[2];
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = dFact.newDocumentBuilder();
Document doc = build.newDocument();
Element root = doc.createElement("root");
doc.appendChild(root);
Element memberList = doc.createElement("members");
root.appendChild(memberList);
for (int i = 0; i < input.length; i++) {
line = input[i].split(",");
Element member = doc.createElement("member");
memberList.appendChild(member);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode(line[0]));
member.appendChild(name);
Element phone = doc.createElement("phone");
phone.appendChild(doc.createTextNode(line[1]));
member.appendChild(phone);
}
TransformerFactory tFact = TransformerFactory.newInstance();
Transformer trans = tFact.newTransformer();

StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
System.out.println(writer.toString());

} catch (TransformerException ex) {
System.out.println("Error outputting document");
} catch (ParserConfigurationException ex) {
System.out.println("Error building document");
}
}
}


Related Topics



Leave a reply



Submit