Skip to Content.
Sympa Menu

mace-opensaml-users - clone an XMLObject

Subject: OpenSAML user discussion

List archive

clone an XMLObject


Chronological Thread 
  • From: Asa Hardcastle <>
  • To: mace-opensaml-users <>
  • Subject: clone an XMLObject
  • Date: Mon, 18 Feb 2008 22:37:18 -0500

Hi All,

I have a situation where I need to pull elements out of one document and put them into a new document, while preserving the original document.

So for example, imagine that I have successfully unmarshalled the following XML:

<tree id="1">
<branch length="45">
<leaf>rustle</leaf>
<bird color="red">chirp chirp</bird>
<branch>
<leaf/>
<monkey/>
</branch>
</branch>
<root>water</root>
</tree>

And I have just created a new XMLObject for the second tree:

<tree id="2"></tree>

I need to copy < branch > wholesale from tree 1 to tree 2. I do not know what elements exist in branch, it implements ElementExtensibleXMLObject and AttributeExtensibeXMLObject.

Marshalling, cloning, and unmarshalling seems to work:

tree2.setBranch( cloneAnXMLObject( tree1.getBranch() ) );

public static XMLObject cloneAnXMLObject(XMLObject xmlObject)
{
Marshaller m = marshallerFactory.getMarshaller(xmlObject);
Element ele = m.marshall(xmlObject);
Element eleClone = (Element)ele.cloneNode(true);
Unmarshaller u = unmarshallerFactory.getUnmarshaller(eleClone);
return u.unmarshall(eleClone);
}

Is there a better or prescribed way? Are there any negative side- effects (besides performance) to the approach above?


thanks,

asa




--
Asa Hardcastle, Technical Lead, openLiberty ID-WSF ClientLib
Tel: +1.413.429.1044 Skype: subsystem7




Archive powered by MHonArc 2.6.16.

Top of Page