use strict;
use XML::XPath;
my $xml = '<SuperItem>
<UserID>1234</UserID>
<MajorItems>
<MinorItem>
<Item>Box</Item>
</MinorItem>
</MajorItems>
<MajorItems>
<MinorItem>
<Item>Suitcase</Item>
</MinorItem>
</MajorItems>
</SuperItem>
';
my $xp = XML::XPath->new( xml => $xml );
my $path = '/SuperItem/MajorItems[2]/MinorItem';
print $xp->findnodes_as_string($path);
Returns:<MinorItem>
<Item>Suitcase</Item>
</MinorItem>
Actually there are several XPath expressions which would give the same result, such as //MajorItems[2]/MinorItem or /SuperItem[UserID='1234']/MajorItems[2]/MinorItem. It will all depend on the actual content and structure of your XML. Update: Changed XML::Xpath to XML::XPath. thx PipTigger!
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|