Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: XML Node Values Based On Attributes

by hippo (Bishop)
on Sep 29, 2022 at 22:13 UTC ( [id://11147161]=note: print w/replies, xml ) Need Help??


in reply to XML Node Values Based On Attributes

There are so many different ways to approach this given the size of the toolkit that is libxml2. Here is one native approach.

#!/usr/bin/env perl use strict; use warnings; use XML::LibXML; my $xml = XML::LibXML->load_xml(location => 'locations.xml'); my $nl = $xml->getElementsByLocalName ('Data'); my @text; for my $node ($nl->get_nodelist) { push @text, $node->textContent if 'NAME_2' eq $node->getAttribute +('name'); } print "$_\n" for @text;

I've structured it this way because to me it is pretty easy to follow and clear what is going on. YMMV.


🦛

Replies are listed 'Best First'.
Re^2: XML Node Values Based On Attributes
by gpjahn (Novice) on Oct 04, 2022 at 11:12 UTC
    Thank you! Nice and simple, I like it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found