KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Here's some code I am having problems with, I process some XML and in a method in an OO class I extract an element from each of several nodes that repeat in the document. There should only be one such element in the subtree for each node but my code gets all elements as if it is operating on the document as a whole. Because I only expected to get oine element I only use the zeroth element of an array, this leads my function to output the wrong value (and its the same for all items in the document) Here's some simplified code that illustrates the problem $ cat t4.pl #!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $xml = <<EndXML; <Envelope> <Body> <Reply> <List> <Item> <Id>8b9a</Id> <Message> <Response> <Identifier>55D</Identifier> </Response> </Message> </Item> <Item> <Id>5350</Id> <Message> <Response> <Identifier>56D</Identifier> </Response> </Message> </Item> </List> </Reply> </Body> </Envelope> EndXML my $foo = Foo->new(); my $parser = XML::LibXML->new(); my $doc = $parser->parse_string( $xml ); my @list = $doc->getElementsByTagName( 'Item' ); for my $item ( @list ) { my $id = get( $item, 'Id' ); my @messages = $item->getElementsByLocalName( 'Message' ); for my $message ( @messages ) { my @children = $message->getChildNodes(); for my $child ( @children ) { my $name = $child->nodeName; if ( $name eq 'Response' ) { print "child is a Response\n"; $foo->do( $child, $id ); } elsif ( $name eq 'text' ) {
Tags (comma-separated)
Save Edits
Cancel