Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When this is your Element retrieved with XPath</p> <pre><code>XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); Element element = (Element) xpath.evaluate( "/container/item/xmlText", document, XPathConstants.NODE); </code></pre> <p>Then, you can do something along these lines:</p> <pre><code>java.io.ByteArrayOutputStream data = new java.io.ByteArrayOutputStream(); java.io.PrintStream ps = new java.io.PrintStream(data); // These classes are part of Xerces. But you will find them in your JDK, // as well, in a different package. Use any encoding here: org.apache.xml.serialize.OutputFormat of = new org.apache.xml.serialize.OutputFormat("XML", "ISO-8859-1", true); org.apache.xml.serialize.XMLSerializer serializer = new org.apache.xml.serialize.XMLSerializer(ps, of); // Here, serialize the element that you obtained using your XPath expression. serializer.asDOMSerializer(); serializer.serialize(element); // The output stream now holds serialized XML data, including tags/attributes... return data.toString(); </code></pre> <p><strong>UPDATE</strong></p> <p>This would be more concise, rather than using Xerces internals. It's the same as Dimitre's solution, just not with an XSLT stylesheet but all in Java:</p> <pre><code>ByteArrayOutputStream out = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Source source = new DOMSource(element); Result target = new StreamResult(out); transformer.transform(source, target); return out.toString(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload