Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: XML Parsing

by tmharish (Friar)
on Jan 31, 2013 at 14:12 UTC ( #1016325=note: print w/replies, xml ) Need Help??


in reply to Re^2: XML Parsing
in thread XML Parsing

Quite simply $hash_when_cat_eq_special is a pointer to a hash. So you can do your normal hash operations on it. The ->pointer() function returns this for you.

In terms of the foreach:

foreach my $elem ( @{ $xml_obj->{list}{value} } ) { $hash_of_subtree = $elem->pointer() ; }

The advantage here is that $xml_obj, $xml_obj->{list} ... are all Objects of type XML::Smart and can always be used as either a hash OR an array.

To extract your data you can simply use the 'pointer' function ( $xml_smart_obj->pointer() )that will return the hash structure or the 'content' function ( $xml_smart_obj->content() ) that will return the contents of a node.

Replies are listed 'Best First'.
Re^4: XML Parsing
by vmallya (Initiate) on Feb 08, 2013 at 14:14 UTC
    Harish, I see that when using content() to return contents of the node and that $xml_smart_obj was always returning one element, whereas the xml has multiple entries for the tag <parameter>. 1. How to list down all the pointers/contents for the tag <parameter> 2. Can I see the array size using $#xml_obj ? Thanks

      ->pointer will actually give you the tree starting from where you have traversed to in the xml_smart object. It will be a hash which you can use like an ordinary hash.

      You can traverse the object using the method described above and it will move the pointer.

      ___________node1 | root ---|-----------node2 |___________node3 ^ |________________Pointer could be here.

      Now when you ask for ->content() it will give you the content at node3 and if you ask for ->pointer it will give you the sub-tree starting from node3 in a hash.

      Hope that helps - Let me know if you need further clarification.

        Harish, Thanks for the clarification. One more doubt, say for node1, node2 i have defined an attribute, can i put a check something like if exists {node}(instance) ? Also how to access the value of attribute if it exists for each node. Regards Vishranth

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016325]
help
Chatterbox?
[ambrus]: Our spam filter rules disallow links to certain domains, and some suspicious pharses that have appeared in previous spam advertising cheap online whatevers.
[LanX]: some servers were lagging today, so I suppose the root cause
[LanX]: ambrus no recent patches
[Petroza]: no i haven't posted anything before. It was a more or less long question with a specific issue. I did post a title and the links i added where only part of the element i was searching within the code (so no purpose other than the question itself).
[1nickt]: Petroza can you go back in your browser to the preview screen, redact the links, and try to submit again?
[LanX]: copy and paste the text into your Petroza's scratchpad please
[Petroza]: yes I'll do that. instead of the url I'll write "link"
[LanX]: darn my pc is restarting for updates

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2017-10-17 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (233 votes). Check out past polls.

    Notices?