in reply to Get all child node values for a particular parent in xml into an array

G'day rahulruns,

I think the method you're looking for is textContent(). XML::LibXML has good documentation. Unsurprisingly, methods relating to nodes can be found in XML::LibXML::Node.

Using the same data for all elements is a poor way to test: you've no way of knowing which "xxx,xxx" relates to which node.

Here's an example of using textContent(): the code starts much as yours does; I've improved the test data.

#!/usr/bin/env perl use strict; use warnings; use XML::LibXML; my $dom = XML::LibXML->load_xml(string => <<'EOT'); <?xml version="1.0" encoding="UTF-8"?> <cidconfig> <createnew> <logon>aaa,aaa</logon> <startnew>bbb,bbb</startnew> <choosecs>ccc,ccc</choosecs> <chooseapp>ddd,ddd</chooseapp> <chooseconfig>eee,eee</chooseconfig> <chooseconftype>fff,fff</chooseconftype> <startnew1>ggg,ggg</startnew1> <activate>hhh,hhh</activate> </createnew> </cidconfig> EOT my $root = $dom->getDocumentElement; my @records = $root->findnodes("//createnew"); my @kids; push @kids, $_->childNodes for @records; print $_->textContent for @kids;

Output:

aaa,aaa bbb,bbb ccc,ccc ddd,ddd eee,eee fff,fff ggg,ggg hhh,hhh

— Ken

Replies are listed 'Best First'.
Re^2: Get all child node values for a particular parent in xml into an array
by NetWallah (Canon) on Sep 01, 2017 at 18:27 UTC
    You can use the xPath to directly grab the children:
    my @records = $root->findnodes("//createnew/*//text()"); print $_->textContent() ,"\n" for @records;

                    All power corrupts, but we need electricity.