English 中文(简体)
采用XML:TreeBuilder
原标题:What is the correct way to parse XML using XML::TreeBuilder
  • 时间:2010-12-02 11:16:00
  •  标签:
  • xml
  • perl

我需要在XML之后教:

<response>
   <entity id="1">
      <exists>Y</exists>
   </entity>

   <entity id="2">
      <exists>Y</exists>

      <attributes>
         <attribute name="User">root</attribute>
      </attributes>

      <links>
         <link type="hard">
            <entity id="1"/>
         </link>
      </links>
   </entity>
</response>

There is two child "entity" elements in "response" element.
But following code will return 3 "entity" elements:

my $tree = XML::TreeBuilder->new();
$tree->parse($responseXML);

my $response = $tree->find_by_tag_name( response );
foreach my $entity ($response->find_by_tag_name( entity ))
{
   print "$entity
";
}

This code returns also "entity" element that is child of "link" element.
But I need to get only "entity" elements that are childs of "response" element.
What is the correct way to do it?
Something like this?

my @elements = $response->content_list();
foreach my $element (@elements)
{
   if (ref($element) && $element->tag() eq "entity")
   {
      #process entity element
      my $id = $element->attr("id");
      print "Entity id=$id
";
   }
}

www.un.org/Depts/DGACM/index_spanish.htm 这是否好?

问题回答

lineage_tag_names() (refer :Element s documentation)将一份内容的祖先名单退回。 第0祖先为母。

Program

#!/usr/bin/env perl

use strict;
use warnings;

use XML::TreeBuilder;

my $tree = XML::TreeBuilder->new();
$tree->parse( *DATA );

my $response = $tree->find_by_tag_name( response );
for my $entity ( $response->find_by_tag_name( entity ) ) {
    my $immediate_parent = ( $entity->lineage_tag_names() )[0];
    if ( $immediate_parent eq  response  ) {
        print $entity->as_XML();
        print  _  x 50, "
";
    }
}

__DATA__
<response>
   <entity id="1">
      <exists>Y</exists>
   </entity>

   <entity id="2">
      <exists>Y</exists>

      <attributes>
         <attribute name="User">root</attribute>
      </attributes>

      <links>
         <link type="hard">
            <entity id="1"/>
         </link>
      </links>
   </entity>
</response>

Output

<entity id="1">
      <exists>Y</exists>
   </entity>
__________________________________________________
<entity id="2">
      <exists>Y</exists>

      <attributes>
         <attribute name="User">root</attribute>
      </attributes>

      <links>
         <link type="hard">
            <entity id="1"></entity>
         </link>
      </links>
   </entity>
__________________________________________________




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签