rahulruns has asked for the wisdom of the Perl Monks concerning the following question:

Have an xml file in which one parent tag has multiple child tags and each of these tags have some value. I need to get all child node values in an array, iterate through each and perform specific task on values. When I try to get records what I am ending up is with entire child node as a single element of an array. How do read each child as an seperate element of an array. What I am using is XML::LibXML

<?xml version="1.0" encoding="UTF-8"?> <cidconfig> <createnew> <logon>xxx,xxx</logon> <startnew>xxx,xxx</startnew> <choosecs>xxx,xxx</choosecs> <chooseapp>xxx,xxx</chooseapp> <chooseconfig>xxx,xxx</chooseconfig> <chooseconftype>xxx,xxx</chooseconftype> <startnew1>xxx,xxx</startnew1> <activate>xxx,xxx</activate> </createnew> </cidconfig> Code I have written use strict; use warnings; use diagnostics; use XML::LibXML; my $dom = XML::LibXML->load_xml(location => $ARGV[0]); my $root = $dom->getDocumentElement; my @records = $root->findnodes("//createnew"); print @records; foreach my $y (@records) { my $x = $y->childNodes; print "$x"; print "Yes\n"; } Output <createnew> <logon>xxx,xxx</logon> <startnew>xxx,xxx</startnew> <choosecs>xxx,xxx</choosecs> <chooseapp>xxx,xxx</chooseapp> <chooseconfig>xxx,xxx</chooseconfig> <chooseconftype>xxx,xxx</chooseconftype> <startnew1>xxx,xxx</startnew1> <activate>xxx,xxx</activate> </createnew> xxx,xxx xxx,xxx xxx,xxx xxx,xxx xxx,xxx xxx,xxx xxx,xxx xxx,xxx Yes
  • Comment on Get all child node values for a particular parent in xml into an array
  • Download Code

Replies are listed 'Best First'.
Re: Get all child node values for a particular parent in xml into an array
by kcott (Archbishop) on Sep 01, 2017 at 05:36 UTC

    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

      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.

Re: Get all child node values for a particular parent in xml into an array
by Anonymous Monk on Sep 01, 2017 at 14:34 UTC
    You will need to apply XPath expressions more than one time, first to obtain the list of <createnew> nodes wherever they occur, then separately (in a loop) for each node that you just found. libxml2 is an industry-standard binary implementation of XML, used by most languages for their XML support, therefore all of the many XPath tutorials that can be found on the web apply to it. Any example that you find for another programming language will apply directly to Perl, too, if you are using this module.