Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

reading from xml files

by valavanp (Curate)
on Mar 26, 2007 at 11:13 UTC ( #606552=perlquestion: print w/ replies, xml ) Need Help??
valavanp has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, i used the following code to get the xml contents from a file. when i use this i am getting only the first para in that file. actually there are 2 paragraphs. Please spot me where i am wrong.
use XML::Twig; my $file = "C:/test2.xml"; my $tmp; $tmp = XML::Twig->new(); $tmp->parsefile($file); my $root = $tmp->root; foreach my $product($root->children('doc')){ my @para=$product->first_child_text('para'); print @para; }
test2.xml
<para>aaaaaaaaaaaaaaaaa dsnbbtejk</para> <para>aldmflskddddddddddddddddddffffffffffffff</para>
</code>

Comment on reading from xml files
Select or Download Code
Re: reading from xml files
by davorg (Chancellor) on Mar 26, 2007 at 11:33 UTC

    Firstly, your sample XML isn't valid XML as it has no root node so I wouldn't expect it to parse at all. I've changed it to this:

    <doc> <para>aaaaaaaaaaaaaaaaa dsnbbtejk</para> <para>aldmflskddddddddddddddddddffffffffffffff</para> </doc>

    Secondly, you seem a little confused about which elements you are looking for. At one point you look for elements called 'doc', but there aren't any elements called that in your sample data.

    I think you want something more like this:

    use strict; use warnings; use XML::Twig; my $file = 'test.xml'; my $tmp; $tmp = XML::Twig->new(); $tmp->parsefile($file); my $root = $tmp->root; foreach my $product ($root->children('para')){ my $para = $product->first_child_text; print "$para\n"; }
    --

    See the Copyright notice on my home node.

    "The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg

Re: reading from xml files
by RatKing (Acolyte) on Mar 26, 2007 at 11:51 UTC
    Ok this is just a thought but I see two equal lines in your file. With this I mean they are both top level, there is no child parent relation for that to be there one would have to be in side the other.
    <parent> <child> <second_child></second_child> </child> </parent>
    Rob.
Re: reading from xml files
by GrandFather (Cardinal) on Mar 26, 2007 at 22:17 UTC

    As others have pointed out your (not quite) XML data is not consistent with your code. The following sample may help:

    use strict; use warnings; use XML::Twig; my $xml = <<XML; <root> <para>aaaaaaaaaaaaaaaaa dsnbbtejk</para> <para>aldmflskddddddddddddddddddffffffffffffff</para> </root> XML my $tmp = XML::Twig->new(); $tmp->parse ($xml); my $root = $tmp->root; print $_->trimmed_text (), "\n" for $root->children ('para');

    Prints:

    aaaaaaaaaaaaaaaaa dsnbbtejk aldmflskddddddddddddddddddffffffffffffff

    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://606552]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-04-16 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls