I might be approaching my problem from a remedial point of
view.
I have included a simple example xml file similar to my
real data file. I have a simple working perl script to
read it.
My problem arises because I have a varying number of
products under each group. I can use first_child to get
the first one, but I don't know how to get each 'product'
after that.
From the documentation, I decided to determine how many of
them there are using within_element.
The documentation also says you must use
generate_ns_name(NAME,URI) to define $gs.
I can't find an example of calling generate_ns_name or
within_element.
My xml file looks like this:
========company.xml=======
<comany>
<group>
<name>dev group 1</name>
<product>product 1</product>
<product>product 2</product>
<product>product 3</product>
<product>product 4</product>
</group>
<group>
<name>dev group 2</name>
<product>product 1</product>
<product>product 2</product>
</group>
</company>
========company.pl========
#!/bin/perl -w
use strict;
use XML::Twig;
my $field= 'group';
my $twig= new XML::Twig;
$twig->parsefile( "company.xml"); # build the twig
my $root= $twig->root; # get the root of the twig
my @group = $root->children; # get the list
foreach my $my_group (@group) # the sorted list
{
print "\nGroup Name: ",$my_group->first_child("name")->text;
print "\nGroup Name: ",$my_group->first_child("product")->text;
}
================
Thanks,
Patrick
patrick_breithaupt( at )mentorg.com
Edit kudra,
2001-09-24
Removed cpde tags around text
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.