我需要在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 这是否好?