http://www.perlmonks.org?node_id=975629


in reply to Stepping up from XML::Simple to XML::LibXML

$channel_template =<<END; <object> <object_name>object_1</object_name> <object_id>059c6c8f-52b1-4d3d-8023-f5d333334456</object_id> <object_admin_state>Enabled</object_admin_state> <sub_object> <sub_object_id>059c6c8f-52b1-4d3d-8023-f5d318f30a63</sub_objec +t_id> </sub_object> </object> END $parser = XML::LibXML->new(); $parser->keep_blanks(0); $doc = $parser->load_xml(string => $channel_template); $doc->setEncoding('UTF-8'); $root = $doc->documentElement();
This gives me access to add/remove/update elements within and below 'object' I was able to add objects easily with code like:
$output_element = $doc->createElement('out');
and then subsequently modify the output element and still maintain the entire XML. Recent, the schema for this object changed and added a wrapper 'object_wrapper' to the XML. Like so:
<object_wrapper> <object> <object_name>object_1</object_name> <object_id>059c6c8f-52b1-4d3d-8023-f5d333334456</object_id> <object_admin_state>Enabled</object_admin_state> <sub_object> <sub_object_id>059c6c8f-52b1-4d3d-8023-f5d318f30a63</sub_o +bject_id> </sub_object> </object> </object_wrapper>
Now my documentElement is objectWrapper, and I need it to traverse to object so my logic will remain the same. I've tried setDocument element by finding the node 'object', like the following, but this eliminates the wrapper element:
$parser = XML::LibXML->new(); $parser->keep_blanks(0); $doc = $parser->load_xml(string => $channel_template); $doc->setEncoding('UTF-8'); $root = $doc->documentElement(); my ($object_element) = $root->findnodes('//object'); $doc->setDocumentElement($channel_element); $root = $doc->documentElement();
Any help would be appreciated. Thanks!