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 the XmlDocument type from System.Xml and do the following:

eg:

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

It seems simple, but how can I do it in Java ? Is it possible to load string in xml using something direct, short and simple, as described above, and not use other methods for this?

Thanks in advance.

+4
source share
4 answers

Try the following:

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

You can go through Oscar with:

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

To convert 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); 
+6
source

You have a choice of tree models in Java - DOM, XOM, JDOM, DOM4J. Many people (including Singh above) use the DOM by default because it is included in the JDK, but it is probably the worst of them, mainly because it is the oldest (it was invented before the namespaces appeared) and because it also trying to do this (HTML, event processing, etc., as well as XML). I would suggest using JDOM2. This should not be difficult if you look at Javadoc to find a method that creates a JDOM2 document from an input stream.

+3
source

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 many other open source libraries available. Here are a few options. Take a look at the docs and see which one meets your needs:

XStream Library is very fast and easy to use. I used it to serialize XML, and I am very pleased with that. If you do not want to use the extrenal library, try the javax.xml.parsers.DocumentBuilder class , which is demonstrated here

0
source

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

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

Source: https://habr.com/ru/post/1441172/


All Articles