Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: XML Parsing

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

in reply to XML Parsing

You can use XML::Smart to do this like so:

my $xml_obj = new XML::Smart( $xml ) ; my $hash_when_cat_eq_special = ( $xml_obj->{list}{value}( 'category', +'eq', 'special' )->pointer() );
$hash_when_cat_eq_special will contain: { "/nodes" => { parameter => 1 }, "/order" => ["category", "parameter"], "category" => "special", "parameter" => { "/order" => ["instance", "CONTENT"], "CONTENT" => "\n nameofparameter1\n + ", "instance" => 0, }, }

Of course you can get more specific as follows:

my $xml_obj = new XML::Smart( $xml ) ; my $param_contents = $xml_obj->{list}{value}( 'category', 'eq', 'speci +al' )->{parameter}->content();
$param_contents will contain: "nameofparameter1"

Replies are listed 'Best First'.
Re^2: XML Parsing
by Arunbear (Parson) on Jan 31, 2013 at 12:24 UTC
    This way of invoking the constructor can be problematic:
    my $xml_obj = new XML::Smart( $xml ) ;
    See the section "Indirect Object Syntax" in Invoking Class Methods
      Do you really use
      new XML::Smart::
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        I'm still using
        Old habits don't go easily ;) (though I always name Modules with an initial upper case character, and functions with a lower case character, and haven't had any problems with this yet).
Re^2: XML Parsing
by vmallya (Initiate) on Jan 31, 2013 at 13:16 UTC
    First of all thank you Harish. I got the output as suggested. Say for example I had few more child elements under value tag, can I use foreach and extract the elements/sub-elements into a hash/array? Could you give me any sample code to put the contents of $hash_when_cat_eq_special in an hash? Thanks.

      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.

        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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016293]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2017-10-18 13:38 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.