Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java xpath to return an entire element as string

Tags:

java

xml

xpath

I need to use java xpath to return by id an xml element as a string.

given...

<svg>
    <g id="Background">
    </g>
    <g id="Outline">
        <polygon fill="none" stroke="#000000" stroke-linecap="round" stroke-linejoin="round"     stroke-miterlimit="10" points=" 119.813,57.875 119.188,57.87" />
    </g>
    <g id="Base_Colour" transform="matrix(0.25 0 0 0.25 0 0)">
        <path fill="#ADB1AF" d="M112.25,208l-8,20.25l-0.5-1.75l0.75-0.5v-1.5l0.75-0.5v-1.5L106,222v-1.5l0.75-0.5v-1.5l0.75-0.5v-1.5"/>
        <path fill="#625595" d="M112.25,208l5.25-14.5l30-30.25l2.25-1.5l41.5-20.5l49.75-9.5h4.25l49,3l48.75"/>
    </g>
</svg>

the value returned needs to be...

<g id="Outline">
    <polygon fill="none" stroke="#000000" stroke-linecap="round" stroke-linejoin="round"     stroke-miterlimit="10" points=" 119.813,57.875 119.188,57.87" />
</g> 

I have googled extensively and nothing I have tried has been able to return the whole element. Xpath is desired because I want to query g tags at any level by id.

like image 950
jeremyjjbrown Avatar asked Dec 19 '12 01:12

jeremyjjbrown


People also ask

What is XPathFactory?

An XPathFactory instance can be used to create XPath objects. See newInstance(String uri) for lookup mechanism. The XPathFactory class is not thread-safe. In other words, it is the application's responsibility to ensure that at most one thread is using a XPathFactory object at any given moment.

How do I select all elements in XPath?

Click on Use XPath Selection, which should cause the Selector text box to appear. 4. This textbox is where you can input an XPath path to select elements on the page.

What does XPath compile do?

Compiles the XPath expression specified and returns an XPathExpression object representing the XPath expression. Compiles the specified XPath expression, with the IXmlNamespaceResolver object specified for namespace resolution, and returns an XPathExpression object that represents the XPath expression.

What is XPath parsing?

It defines a language to find information in an XML file. It is used to traverse elements and attributes of an XML document. XPath provides various types of expressions which can be used to enquire relevant information from the XML document.


2 Answers

The solution I found was to get the org.w3c.dom.Node with xpath (DOM would work too). Then I created a javax.xml.transform.dom.DOMSource from the node and transformed that to a string with javax.xml.transform.TransformerFactory.

Node node = // the node you want to serialize
xmlOutput = new StreamResult(new StringWriter());
transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), xmlOutput);
String nodeAsAString = xmlOutput.getWriter().toString();

This is easily factored into a class for reuse. Unfortunately, the is no .OuterXml property in Java as there is in .NET. All you .NETer's can smirk now.

like image 91
jeremyjjbrown Avatar answered Oct 30 '22 10:10

jeremyjjbrown


No xpath will return a string containing XML syntax, ever.

like image 38
bmargulies Avatar answered Oct 30 '22 09:10

bmargulies