15.03.2020 Views

perl-language-es

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

my $root = $dom->getDocumentElement;

# if the document has children

if($root->hasChildNodes) {

# getElementsByLocalName returns a node list of all elements who's

# localname matches 'title', and we want the first occurrence

# (via get_node(1))

my $title = $root->getElementsByLocalName('title');

if(defined $title) {

# Get the first matched node out of the nodeList

my $node = $title->get_node(1);

# Get the text of the target node

my $title_text = $node->textContent;

}

print "The first node with name 'title' contains: $title_text\n";

}

# The above calls can be combined, but is possibly prone to errors

# (if the getElementsByLocalName() failed to match a node).

#

# my $title_text = $root->getElementsByLocalName('title')->get_node(1)->textContent;

# Using Xpath, get the price of the book with id 'bk104'

#

# Set our xpath

my $xpath = q!/catalog/book[@id='bk104']/price!;

# Does that xpath exist?

if($root->exists($xpath)) {

# Pull in the twig

my $match = $root->find($xpath);

if(defined $match) {

# Get the first matched node out of the nodeList

my $node = $match->get_node(1);

# pull in the text of that node

my $match_text = $node->textContent;

}

}

print "The price of the book with id bk104 is: $match_text\n";

Lea Análisis XML en línea: https://riptutorial.com/es/perl/topic/3590/analisis-xml

https://riptutorial.com/es/home 11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!