Oscar Jara
Oscar Jara

Reputation: 14187

Is there a Java equivalent for XmlDocument.LoadXml() from .NET?

In .NET C#, when trying to load a string into xml, you need to use XmlDocument type from System.Xml and do the following:

e.g:

string xmlStr = "<name>Oscar</name>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
Console.Write(doc.OuterXml);

This seems simple but how can I do this in Java? Is it possible to load a string into xml using something directly, short and simple like above and avoid implementing other methods for this?

Thanks in advance.

Upvotes: 4

Views: 9577

Answers (4)

Michael Kay
Michael Kay

Reputation: 163595

You have a choice of tree models in Java - DOM, XOM, JDOM, DOM4J. Many people (including Singh above) use DOM by default because it is included in the JDK, but it's probably the worst of the bunch, largely because it's the oldest (it was invented before namespaces came along), and because it tries to do too much (HTML, event handling etc, as well as XML). I'd suggest using JDOM2. It shouldn't be hard if you look at the Javadoc for you to find the method that builds a JDOM2 document from an input stream.

Upvotes: 3

Yogendra Singh
Yogendra Singh

Reputation: 34387

Try this:

DocumentBuilderFactory documentBuildFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder doccumentBuilder = documentBuildFactory.newDocumentBuilder();
Document document = 
 doccumentBuilder.parse(new ByteArrayInputStream("<name>Oscar</name>".getBytes()));

You can traverse Oscar by:

String nodeText = document.getChildNodes().item(0).getTextContent() ;         
System.out.println(nodeText);

To transaform back:

TransformerFactory tFactory =  TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
//to print the string in sysout, System.out
StreamResult streamResult = new StreamResult(System.out);
transformer.transform(domSource, streamResult ); 

To get the result in String:

DOMSource source = new DOMSource(document);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(outStream);
transformer.transform(source, result);
String resultString = new String( outStream.toByteArray());
System.out.println(resultString);

Upvotes: 6

Anshu
Anshu

Reputation: 7853

If you want to parse a String str to Document in Java, you can do following:

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        return docBuilder.parse(is);

Upvotes: 0

Thorn
Thorn

Reputation: 4057

Java has a ton of libraries for working with XML. In addition to the many classes that work with XML included with the standard Java installation, there are lots of other open source libraries available. Here are a few options. Take a look at the docs and see which one meets your needs:

The XStream Library is very fast and easy to use. I've used it for XML serialization and I'm very happy with it. If you would rather not use an extrenal library, then try the javax.xml.parsers.DocumentBuilder class that is demonstrated here

Upvotes: 0

Related Questions