desau wrote:I want to run xpath on a node that's been cloned with the XmlUtils.deepCopy method. Is this possible?
Yes, it is possible.
deepCopy works by marshalling to a byte array, then unmarshalling.
- Code: Select all
Marshaller mar = jc.createMarshaller();
ByteArrayOutputStream bout = new ByteArrayOutputStream(256);
mar.marshal(elem, bout);
Unmarshaller unmar = jc.createUnmarshaller();
elem = unmar.unmarshal(new StreamSource(new ByteArrayInputStream(
bout.toByteArray())), valueClass);
Looking at the code in MainDocumentPart.unmarshal, which is what makes the XPath stuff possible, you need something like:
- Code: Select all
binder = jc.createBinder();
jaxbElement = binder.unmarshal( node );
So you'll need to copy the deepCopy code into a new method, marshall to a node rather than a byte array, and then do binder.unmarshall.
You then have everything you need to invoke XmlUtils.getJAXBNodesViaXPath
Pretty straightforward. The only thing is you want 2 return objects, the clone and the binder.
You could pass a binder into your method, or you could create a new class which will encapsulate your clone and the binder.
Please post your resulting code. Thanks.
desau wrote:I want to modify content of the nodes that are returned from xpath, then eventually add the cloned nodes back to the document that I'm working on. Is this possible?
I think this should "just work".