Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Get data from an XML file heading

by nachtmsk (Acolyte)
on Feb 20, 2020 at 15:58 UTC ( [id://11113254]=note: print w/replies, xml ) Need Help??


in reply to Re: Get data from an XML file heading
in thread Get data from an XML file heading

Thanks. I didn't think it looked like XML Because it didn't have an ending and it has multiple values for one, node, I guess you would call it.

I thought XML looks more like below...

<myId>757</myId> <address> 445 smith</address>

The data I get at the top of the file has one node name "result_summary" no ending tag/node and multiple values within the node.

Thanks for the solution though.
  • Comment on Re^2: Get data from an XML file heading

Replies are listed 'Best First'.
Re^3: Get data from an XML file heading
by haukex (Archbishop) on Feb 20, 2020 at 16:13 UTC
    I didn't think it looked like XML Because it didn't have an ending and it has multiple values for one, node, I guess you would call it.

    It's known as an "empty-element" tag, <element /> is equivalent to <element></element>, and the values within the tag itself are attributes. See e.g. https://www.w3schools.com/xml/.

      Thanks again.

      One more thing. Not sure if I understand what this line of code is doing. It looks like it's taking the first element of the array called 'nodes' and converting it into a hash called attrs. Is that correct?

      my %attrs = %{ $nodes[0] };
        It looks like it's taking the first element of the array called 'nodes' and converting it into a hash called attrs. Is that correct? my %attrs = %{ $nodes[0] };

        Almost right, it's not exactly converting, it's doing a hash dereference, see perlreftut and perlref. Basically:

        my %hash = ( foo => "bar" ); my $hashref = \%hash; # take a reference to the hash print $hashref->{foo},"\n"; # acccess a value via the ref my %attrs = %{ $hashref }; # makes a shallow copy

        (In this case, there's a slightly more complicated thing going on in the background, that you don't necessarily need to care about. $nodes[0] is an object, which provides a special behavior when you use it like a hash reference: it returns the attributes of the element as a hash. That's why I can use this object as if it were a hash reference and dereference it, even though it's a much more complex object than just the attributes in a hash - I can call methods on it, etc. It's documented in XML::LibXML::Element under "Overloading".)

Log In?
Username:
Password:

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

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

    No recent polls found