Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by Anonymous Monk
on Jun 11, 2012 at 19:56 UTC ( [id://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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found