Hi everyone,
I'm new with Perl and tried to parse the following xml file. I try thing's like libXML and other thing's, but I couldn't get values like code_a / name. Can someone Help me with this.
Thanks in Advance
Tiago
<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envel
+ope/">
<soapenv:Body>
<dlwmin:getBookById xmlns:dlwmin="http://www.test.com/integration"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result xmlns="http://www.test.com/integration/integration">
<status>
<state>0</state>
</status>
<books>
<num>12345</num>
<book>
<code_a>11111</code_a>
<name>Come to the Dark Side 1st Ed</name>
<otherVal>ABC</otherVal>
<otherVal2>1999</otherVal2>
</book>
<book>
<code_a>22222</code_a>
<name>Come to the Dark Side 2nd Ed</name>
<otherVal>ABC</otherVal>
<otherVal2>2001</otherVal2>
</book>
<Title>Come to the Dark Side</Title>
<typeOfBook>SciFi</typeOfBook>
<writer>Darth Vader</writer>
</books>
</result>
</dlwmin:getBookById>
</soapenv:Body>
</soapenv:Envelope>
Like I said before I'm new with perl :) 4 hours, I need to parse a lot of files in a Production Env, I tried the code bellow:
use strict;
use warnings;
use XML::LibXML;
use XML::LibXML qw( );
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file('out.xml');
for my $result ($doc->findnodes('/books/book')) {
for my $interv ($result->findnodes('book')) {
my $bk = $interv->find('code_a');
my $bk_name = $interv->find('name');
print("$bk - $bk_name");
}
}
and this one (I couldn't understand how to interate with the hash):
use strict;
use XML::Simple;
use Data::Dumper;
my $doc = XMLin('out.xml');
print Dumper($doc);
antoher thing the xml was in one line.
thanks
In reply to XML Parse
by tfeitor
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.