parsing XML result by using perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing XML result by using perl?
# 1  
Old 08-15-2012
parsing XML result by using perl?

for some reasons, i need to parse the XML result by using perl.
for instance, this is a sample XML result:

Code:
<Response>
<status>success</status>
<answer>AAA::AAA</answer>
<answer>BBB::BBB</answer>
</Response>

then i can use this way :

Code:
my @output = ();
foreach my $parts (@all)    ##@all = above XML result
{
  chomp($parts);

  if ($parts =~ /\<answer\>(.*)\<\/answer\>/)
  {
     push @output, $1;
  }
}

Then i can get a list for all the answers.
BUT i have problem in processing multiple sections in XML result, something like :
Code:
<Response>
<status>success</status>
<item key=1>
<answer>AAA::AAA</answer>
<answer>BBB::BBB</answer>
</item>
<item key=2>
<answer>CCC::CCC</answer>
<answer>DDD::DDD</answer>
</item>
</Response>

any advice on this? or is there any existing module for this parsing?
Thanks.
# 2  
Old 08-15-2012
Take a look at XML::XPath module.
# 3  
Old 08-15-2012
Quote:
Originally Posted by tiger2000
...BUT i have problem in processing multiple sections in XML result, something like :
Code:
<Response>
<status>success</status>
<item key=1>
<answer>AAA::AAA</answer>
<answer>BBB::BBB</answer>
</item>
<item key=2>
<answer>CCC::CCC</answer>
<answer>DDD::DDD</answer>
</item>
</Response>

...
Code:
$
$
$ cat f67.xml
<Response>
<status>success</status>
<item key=1>
<answer>AAA::AAA</answer>
<answer>BBB::BBB</answer>
</item>
<item key=2>
<answer>CCC::CCC</answer>
<answer>DDD::DDD</answer>
</item>
</Response>
$
$
$ perl -lne 'if (/\<item key=(.*?)\>/) { $key = $1 }
             elsif (/\<answer\>(.*?)\<\/answer\>/) { push @{$x{$key}}, $1 }
             END {
               while ( ($k, $v) = each %x) {
                 print "Key = $k";
                 foreach $i (@$v) { print "\tAnswer = $i" }
               }
             }
            ' f67.xml
Key = 1
        Answer = AAA::AAA
        Answer = BBB::BBB
Key = 2
        Answer = CCC::CCC
        Answer = DDD::DDD
$
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

2. Shell Programming and Scripting

Help in parsing XML output file in perl.

Hi I have an XML output like : <?xml version="1.0" encoding="ISO-8859-1" ?> - <envelope> - <body> - <outputGetUsageSummary> - <usgSumm rerateDone="5"> - <usageAccum accumId="269" accumCaptn="VD_DP_AR" inclUnits="9999999.00" inclUnitsUsed="0.00" shared="false" pooled="false"... (7 Replies)
Discussion started by: rkrish
7 Replies

3. Shell Programming and Scripting

Parsing XML

I am trying to parse an xml file and trying to grab certain values and inserting them into database table. I have the following xml that I am parsing: <dd:service name="locator" link="false"> <dd:activation mode="manual" /> <dd:run mode="direct_persistent" proxified="false" managed="true"... (7 Replies)
Discussion started by: $criptKid617
7 Replies

4. UNIX for Advanced & Expert Users

XML Parsing

I had a big XML and from which I have to make a layout as below *TOTAL+CB | *CB+FX | CS |*IR | *TOTAL | -------------------------------------------------------------------------------------------------- |CB FX | | | | DMFXNY EMSGFX... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

5. Shell Programming and Scripting

Bash XML Parsing using Perl XPath

I have a bash script that needs to read input from an XML file, which includes varying numbers of a certain type of child node. I want to be able to iterate through all the child nodes of a given parent. I installed the Perl XML-XPath package from search.cpan.org. Once it's installed, from bash,... (4 Replies)
Discussion started by: jfmorales
4 Replies

6. Shell Programming and Scripting

Random XML Parsing - using Perl

Given the XML: <?xml version="1.0" encoding="UTF-8"?> <reference> <refbody> <section> <p> <ul> <li><xref href="file1.dita#anchor" /></li> <li><xref href="file2.dita#anchor" /></li> </ul> </p> </section> <section> <p> <xref href="file3.dita#anchor" /> </p> <p> <xref... (4 Replies)
Discussion started by: ricksj
4 Replies

7. Shell Programming and Scripting

XML Parsing

Hi, Need a script to parse the following xml file content <tag1 Name="val1"> <abc Name="key"/> <abc Name="pass">*********</abc> </tag1> <tag2 Name="Core"> <Host Name="a.b.c"> <tag1 Name="abc"> <abc Name="ac">None</abc> ... (4 Replies)
Discussion started by: Mavericc
4 Replies

8. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

9. UNIX for Advanced & Expert Users

xml parsing error in perl

******************PERL VERSION************************ This is perl, v5.8.1 built for i386-linux-thread-multi ERROR!!!!---Undefined subroutine &main::start called at /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/XML/Parser/Expat.pm line 469. *********************PERL... (1 Reply)
Discussion started by: bishweshwar
1 Replies

10. Shell Programming and Scripting

parsing xml

I want to use wget comment to parse an xml parse that exist in an online website. How can I connect it using shell script through Unix and how can I parse it?? (1 Reply)
Discussion started by: walnut
1 Replies
Login or Register to Ask a Question