"Gavin Henry" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On Saturday 02 Apr 2005 04:29, Johnstone, Colin wrote:
> > Gidday All,
> >
> > I would like to use xml Parser to parse this chunk of xml (below) and
> > return the business unit name and id attributes for each of the elements
> > where division id = '221'
> >
> > I have tried
>
> Why not try:
>
> http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm
>
> If all you want to do is grab a business unit, follow the example on the
page
> above.

Stick with XPath. Its definitely easier than trying to deciper the
datastructure created by XML::Simple. The XPath query is as simple as:

//[EMAIL PROTECTED]'221']/@name

See below for solution

> >
> > #!/web/Interwoven/TeamSite/iw-perl/bin/iwperl
> >
> > use XML::XPath;
> > use XML::XPath::XMLParser;
> >
> > my $xpath;
> > my $nodeset;
> > my @memberList;
> > my $arg;
> > my $argVal;
> >
> > my $fileToParse =
> > qq[/web/Interwoven/TeamSite/custom/config/DET_structure.xml];
> >
> > $xpath       = XML::XPath->new( filename => "$fileToParse" );
> > $nodeSet     = $xpath->find( $xpathQuery );

Where is this $xpathQuery defined? I dont see it anywhere. Are you not using
the warnings and strict pragmas.

> > @memberList  = map { XML::XPath::XMLParser::as_string( $_ ) }
> > $nodeSet->get_nodelist();
> > @memberList  = map { s|^.*(?=htdocs)||; $_; } @memberList;
> > @memberList  = map { s|//|/|g; $_; } @memberList;
> > # %masterList  = map {  $_, 1  } @memberList;
> >
> > while( @memberList ){
> >
> > print( $_ . '\n' );
> >
> > }
> >

You are working too hard. Once you decide to go with XPath, stick with it.
It can do everything and more that regular expressions can do to xml, and
its easier to read.

Output of the following program:

C:\temp>perl xp.pl
HR Strategy and Information Policy and Reporting - name may change

# xp.pl
use warnings;
use strict;

use XML::XPath;
use Data::Dumper;

my $query = q|//[EMAIL PROTECTED]'221']/@name|;
my $xp = XML::XPath->new( xml => join('', <DATA>) );
my $node_set = $xp->find( $query );

foreach my $node ( $node_set->get_nodelist ) {
  print(  $node->getNodeValue, "\n" );
}

# the loop can simplify to:
# foreach my $node ( $xp->find( $query )->get_nodelist ) {
# rendering the $node_set variable unnecessary

__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<organisation lastUpdate="21/03/05">
  <division name="Office of the Director-General" id="202">
    <business-unit name="Correspondence unit" id="203"/>
  </division>
  <division name="Office of the Deputy Director-General" id="276"/>
    <division name="Strategic Communications and Marketing" id="204">
      <business-unit name="Communications unit" id="1002"/>
    </division>
    <division name="Strategic Legal  Unit" id="1003"/>
    <division name="Internal Audit" id="222"/>
    <division name="Strategic Infrastructure Initiatives" id="1022"/>
    <division name="Human Resource Strategy and Performance" id="221">
      <business-unit name="Workforce Capability" id="214"/>
      <business-unit name="Ethics" id="214C"/>
      <business-unit name="eDRMS Project" id="1024"/>
      <business-unit name="Workforce Management" id="216"/>
      <business-unit name="Strategic Legal and FOI" id="1003"/>
      <business-unit name="HR Strategy and Information Policy and
Reporting - name may change" id="221">
        <sub-business-unit name="HR Business/Workforce Planning" id="1024"/>
        <sub-business-unit name="HR Strategy" id="1025"/>
        <sub-business-unit name="HR Governance/Governance Structures"
id="1026"/>
        <sub-business-unit name="HR Reporting" id="1027"/>
        <sub-business-unit name="HR Business Performance and Analysis"
id="1028"/>
        <sub-business-unit name="HR Information System Protocols"
id="1029"/>
      </business-unit>
    <business-unit name="Office of the GM (HR)" id="1005"/>
  </division>
</organisation>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to