Author

How do I load an org.w3c.dom.Document from XML in a string?

Instead, use the parse method that takes an InputSource, which can be constructed with a Reader, like this: It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k. It’s mostly the same solution as before, augmented to get the root element down as a NodeList and using erickson’s suggestion of using an InputSource instead for character encoding issues.

How do I load an org.w3c.dom.Document from XML in a string?

I have a complete XML document in a string and would like a Document object. Google turns up all sorts of garbage. What is the simplest solution? (In Java 1.5)

Solution Thanks to Matt McMinn, I have settled on this implementation. It has the right level of input flexibility and exception granularity for me. (It’s good to know if the error came from malformed XML — SAXException — or just bad IO — IOException .)

public static org.w3c.dom.Document loadXMLFrom(String xml) throws org.xml.sax.SAXException, java.io.IOException < return loadXMLFrom(new java.io.ByteArrayInputStream(xml.getBytes())); >public static org.w3c.dom.Document loadXMLFrom(java.io.InputStream is) throws org.xml.sax.SAXException, java.io.IOException < javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); javax.xml.parsers.DocumentBuilder builder = null; try < builder = factory.newDocumentBuilder(); >catch (javax.xml.parsers.ParserConfigurationException ex) < >org.w3c.dom.Document doc = builder.parse(is); is.close(); return doc; > 

There’s a potentially serious problem with this code, because it ignores the character encoding specified in the String (which is UTF-8 by default). When you call String.getBytes() the platform default encoding is used to encode Unicode characters to bytes. So, the parser may think it’s getting UTF-8 data when in fact it’s getting EBCDIC or something… not pretty!

Instead, use the parse method that takes an InputSource, which can be constructed with a Reader, like this:

import java.io.StringReader; import org.xml.sax.InputSource; … return builder.parse(new InputSource(new StringReader(xml))); 

It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k.

This works for me in Java 1.5 — I stripped out specific exceptions for readability.

import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import java.io.ByteArrayInputStream; public Document loadXMLFromString(String xml) throws Exception

Just had a similar problem, except i needed a NodeList and not a Document, here’s what I came up with. It’s mostly the same solution as before, augmented to get the root element down as a NodeList and using erickson’s suggestion of using an InputSource instead for character encoding issues.

private String DOC_ROOT="root"; String xml=getXmlString(); Document xmlDoc=loadXMLFrom(xml); Element template=xmlDoc.getDocumentElement(); NodeList nodes=xmlDoc.getElementsByTagName(DOC_ROOT); public static Document loadXMLFrom(String xml) throws Exception

To manipulate XML in Java, I always tend to use the Transformer API:

import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; public static Document loadXMLFrom(String xml) throws TransformerException

Xml — org.w3c.dom.Document in JAVA, In addition to Robby’s answer: you should find sub-elements by tag name instead of just getting child nodes by index. These text nodes might disappear if the XML is …

Читайте также:  background

Problem with conversion of org.dom4j.Document to org.w3c.dom.Document and XML Signature

I have some classes that already use DOM4J to read XML files and provide getter methods to the data. Now, I need to add the possibility of checking XML digital signatures.

Using org.w3c.dom and following http://java.sun.com/developer/technicalArticles/xml/dig_signature_api/ everything works correctly.

So, I try to use DOMWriter to convert from org.dom4j.Document to org.w3c.dom.document, but after this the signature validation doesn’t work. I think it happens because DOMWiter is changing the XML tree (as doc4.asxml() seems to show).

I try to find something to set in order to mantain the integrity of the document, but DOMWriter don’t have such methods.

Below is the code demonstrating the asymmetric conversion.

The file used for tests is http://www.robertodiasduarte.com.br/files/nfe/131090007910044_v1.10-procNFe.xml

Does someone know reasons/workarounds to this?

Thanks (and sorry my poor english).

package testevalidanfe; import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import javax.swing.JOptionPane; import javax.xml.crypto.dsig.XMLSignature; import javax.xml.crypto.dsig.XMLSignatureFactory; import javax.xml.crypto.dsig.dom.DOMValidateContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.dom4j.io.XMLWriter; import org.w3c.dom.Document; import org.w3c.dom.Node; public class Testevalidanfe < public static void main(String[] args) throws Exception < DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document d = db.parse("exemplo-nfe.xml"); Node no = d.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").item(0); DOMValidateContext valContext = new DOMValidateContext(new X509KeySelector(), no); XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); XMLSignature signature = fac.unmarshalXMLSignature(valContext); JOptionPane.showMessageDialog(null, "Validation using org.w3c.dom: " + signature.validate(valContext)); org.dom4j.io.DOMReader domreader = new org.dom4j.io.DOMReader(); org.dom4j.Document doc4 = domreader.read(d); org.dom4j.io.DOMWriter domwriter = new org.dom4j.io.DOMWriter(); d = domwriter.write(doc4); String after = doc4.asXML(); PrintWriter writer = new PrintWriter(new File("after-convertion.xml")); writer.print(after); writer.close(); no = d.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").item(0); valContext = new DOMValidateContext(new X509KeySelector(), no); fac = XMLSignatureFactory.getInstance("DOM"); signature = fac.unmarshalXMLSignature(valContext); JOptionPane.showMessageDialog(null, "Validation after convert: " + signature.validate(valContext)); >> package testevalidanfe; import java.security.Key; import java.security.PublicKey; import java.security.cert.X509Certificate; import java.util.Iterator; import javax.xml.crypto.AlgorithmMethod; import javax.xml.crypto.KeySelector; import javax.xml.crypto.KeySelectorException; import javax.xml.crypto.KeySelectorResult; import javax.xml.crypto.XMLCryptoContext; import javax.xml.crypto.XMLStructure; import javax.xml.crypto.dsig.SignatureMethod; import javax.xml.crypto.dsig.keyinfo.KeyInfo; import javax.xml.crypto.dsig.keyinfo.X509Data; public class X509KeySelector extends KeySelector < public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException < Iterator ki = keyInfo.getContent().iterator(); while (ki.hasNext()) < XMLStructure info = (XMLStructure) ki.next(); if (!(info instanceof X509Data)) continue; X509Data x509Data = (X509Data) info; Iterator xi = x509Data.getContent().iterator(); while (xi.hasNext()) < Object o = xi.next(); if (!(o instanceof X509Certificate)) continue; final PublicKey key = ((X509Certificate)o).getPublicKey(); if (algEquals(method.getAlgorithm(), key.getAlgorithm())) < return new KeySelectorResult() < public Key getKey() < return key; >>; > > > throw new KeySelectorException("No key found!"); > static boolean algEquals(String algURI, String algName) < if ((algName.equalsIgnoreCase("DSA") && algURI.equalsIgnoreCase(SignatureMethod.DSA_SHA1)) || (algName.equalsIgnoreCase("RSA") && algURI.equalsIgnoreCase(SignatureMethod.RSA_SHA1))) < return true; >else < return false; >> > 

For example, if the original XML starts with:

I had a closer look at this, and it turns out that DOM4J DOMWriter is doing something odd w.r.t. namespaces that obviously confuses the canonicalization process. I haven’t pin pointed the exact reason, but I think it has to do with DOMWriter inserting extra xmlns attributes in the DOM elements. You can see the effect if you turn on logging for the XML digital signature API (as described in the article you refer to), the canonicalized element lacks namespace declaration in the DOM document produced by DOM4J.

Читайте также:  PHP Chunk File Upload using Javascript - Mywebtuts.com

However, instead of using DOMWriter, you can produce a dom document by transformation, using a DOM4J documentSource and a DOMResult.

/** * Create a DOM document from a DOM4J document */ static Document copy(org.dom4j.Document orig) < try < TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMResult result = new DOMResult(); t.transform(new DocumentSource(orig), result); return (Document) result.getNode(); >catch (Exception e) < throw new RuntimeException(e); >> 

Using the resulting DOM document, the validation works.

IDocument Interface, Java documentation for org.w3c.dom.Document. Portions of this page are modifications based on work created and shared by the Android Open Source …

Java: how to locate an element via xpath string on org.w3c.dom.document

How do you quickly locate element/elements via xpath string on a given org.w3c.dom.document? there seems to be no FindElementsByXpath() method. For example

I found that recursively iterating through all the child node levels to be quite slow when there are lot of elements of same name. Any suggestions?

I cannot use any parser or library, must work with w3c dom document only.

//obtain Document somehow, doesn't matter how DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); org.w3c.dom.Document doc = b.parse(new FileInputStream("page.html")); //Evaluate XPath against Document itself XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList)xPath.evaluate("/html/body/p/div[3]/a", doc, XPathConstants.NODESET); for (int i = 0; i

With the following page.html file:

How to marshal a JAXB object to org.w3c.dom.Document?, Turns out that the problem was not marshalling from JAXB to document. The problem was that, for some reason, namespace agnostic XPath …

Источник

Java Convert String to XML DOM Example

To convert XML string to XML Dom, we need the following classes:

  • javax.xml.parsers.DocumentBuilder : Defines the API to obtain XML DOM Document instances from XML content from various input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.
  • javax.xml.parsers.DocumentBuilderFactory : Defines a factory API that enables applications to obtain a parser ( DocumentBuilder ) that produces DOM object trees from XML content.
  • org.w3c.dom.Document : It represents the entire XML DOM. Conceptually, it is the root of the document tree, and provides the access to the document’s data further down into the tree, through factory methods.
  • java.io.StringReader : Create a stream from String content. DocumentBuilder uses this stream to read XML content for parsing.
import java.io.StringReader; 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; import org.xml.sax.InputSource; public class ConvertStringToXML < public static void main(String[] args) < final String xmlStr + " Lokesh Gupta" + " " + " " + " Brian Lara" + " " + " " + ""; //Use method to convert XML string content to XML Document object Document doc = convertStringToXMLDocument(xmlStr); //Verify XML document is build correctly System.out.println("Root Node : " + doc.getFirstChild().getNodeName()); NodeList nodeList = doc.getElementsByTagName("employee"); for (int itr = 0; itr < nodeList.getLength(); itr++) < Node node = nodeList.item(itr); System.out.println("\nNode Name : " + node.getNodeName()); if (node.getNodeType() == Node.ELEMENT_NODE) < Element eElement = (Element) node; System.out.println("Name: "+ eElement.getElementsByTagName("name").item(0).getTextContent()); System.out.println("Title: "+ eElement.getElementsByTagName("title").item(0).getTextContent()); >> > private static Document convertStringToXMLDocument(String xmlString) < //Parser that produces DOM object trees from XML content DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //API to obtain DOM Document instance DocumentBuilder builder = null; try < //Create DocumentBuilder with default configuration builder = factory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new InputSource(new StringReader(xmlString))); return doc; >catch (Exception e) < e.printStackTrace(); >return null; > >
Root Node : employees Node Name : employee Name: Lokesh Gupta Title: Author Node Name : employee Name: Brian Lara Title: Cricketer

2. Convert XML File to XML Document

Читайте также:  Css div align center top

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.

  Lokesh Gupta   Brian Lara   
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class StringtoXMLExample < public static void main(String[] args) < final String xmlFilePath = "employees.xml"; //Use method to convert XML string content to XML Document object Document doc = convertXMLFileToXMLDocument( xmlFilePath ); //Verify XML document is build correctly System.out.println(doc.getFirstChild().getNodeName()); >private static Document convertXMLFileToXMLDocument(String filePath) < //Parser that produces DOM object trees from XML content DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //API to obtain DOM Document instance DocumentBuilder builder = null; try < //Create DocumentBuilder with default configuration builder = factory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new File(filePath)); return doc; >catch (Exception e) < e.printStackTrace(); >return null; > >

Drop me your questions in the comments section.

Источник

Java Convert String to XML Document and XML Document to String

Java Convert String to XML Document and XML Document to String

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

  1. Document convertStringToDocument(String xmlStr) : This method will take input as String and then convert it to DOM Document and return it. We will use InputSource and StringReader for this conversion.
  2. String convertDocumentToString(Document doc) : This method will take input as Document and convert it to String. We will use Transformer , StringWriter and StreamResult for this purpose.
package com.journaldev.xml; import java.io.StringReader; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; 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.xml.sax.InputSource; public class StringToDocumentToString < public static void main(String[] args) < final String xmlStr = "\n"+ "DeveloperMale"; Document doc = convertStringToDocument(xmlStr); String str = convertDocumentToString(doc); System.out.println(str); > private static String convertDocumentToString(Document doc) < TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try < transformer = tf.newTransformer(); // below code to remove XML declaration // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; >catch (TransformerException e) < e.printStackTrace(); >return null; > private static Document convertStringToDocument(String xmlStr) < DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try < builder = factory.newDocumentBuilder(); Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) ); return doc; >catch (Exception e) < e.printStackTrace(); >return null; > > 

When we run above program, we get the same String output that we used to create DOM Document.

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.

Источник

Оцените статью