Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
There's more than one way to do things
 
PerlMonks  

Re: Stepping up from XML::Simple to XML::LibXML

by Anonymous Monk
on Jun 11, 2012 at 19:56 UTC ( #975629=note: print w/ replies, xml ) Need Help??


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!


Comment on Re: Stepping up from XML::Simple to XML::LibXML
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://975629]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-04-21 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls