Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML::Lib - Getting values from XML Node

Status
Not open for further replies.

dmazzini

Programmer
Jan 20, 2004
480
0
0
US
Hi guys

Not very active programming these days, but I have a question about one script using XML::Lib

XML Looks like this, I need to get type, uri and href from "target" using XML::Lib.

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tags size="3" uri="XXXXXX">
<tag href="ZZZZZZZZ" uri="DDDDDDDD">
<id>2752699</id>
<name>MYNAME</name>
<type>public</type>
<targets uri="ZZZZZZZ">
<target type="I WANT THIS" uri="I WANT THIS" href="I WANT THIS"/>
<target type="I WANT THIS" uri="I WANT THIS" href= I WANT THIS"/>
</targets>
</tag>

Code:
    my $xs = XML::LibXML->new();
    my $doc = $xs->parse_string($xmldata);
    my $xc = XML::LibXML::XPathContext->new($doc);
    my $tagList = $xc->find('/tags/tag');
  
    foreach my $tag($tagList->get_nodelist) {
    	    my $tagHref = $tag->XML::LibXML::Element::getAttribute('href');  # OK
	        my $tagUri = $tag->XML::LibXML::Element::getAttribute('uri'); # OK
	        my $tagId = $tag->XML::LibXML::Element::getChildrenByTagName('id'); # Ok
	        my $name = $tag->XML::LibXML::Element::getChildrenByTagName('name'); # Ok
	        my $type = $tag->XML::LibXML::Element::getChildrenByTagName('type'); Ok
	        my $target = $tag->XML::LibXML::Element::getChildrenByTagName('target'); Not Ok, How to?	        	        
   }


dmazzini
GSM/UMTS System and Telecomm Consultant

 
I forgot to add this part to the original post, any help would be very appreciated~


Code:
use XML::LibXML;

dmazzini
GSM/UMTS System and Telecomm Consultant

 
Solution: In case somebody need it:

Code:
foreach my $tag($tagList->get_nodelist) {
    	       $tagHref = $tag->XML::LibXML::Element::getAttribute('href');
	           $tagUri = $tag->XML::LibXML::Element::getAttribute('uri');
	           $tagId = $tag->XML::LibXML::Element::getChildrenByTagName('id');
	           $name = $tag->XML::LibXML::Element::getChildrenByTagName('name');
	           $type = $tag->XML::LibXML::Element::getChildrenByTagName('type');
	           #print "$tagHref,$tagUri,$tagId,$name,$type ==>";
	           print "=====================================================\n";
	           print "TAGNAME $name:\n";

               my $targetList = $xc->find('targets/target', $tag);
               foreach my $target($targetList->get_nodelist) {
                       $targetType = $target->XML::LibXML::Element::getAttribute('type');
                       $targetUri = $target->XML::LibXML::Element::getAttribute('uri');
                       $targetHref = $target->XML::LibXML::Element::getAttribute('href');
                       print "$tagHref,$tagUri,$tagId,$name,$type,$targetType, $targetUri,$targetHref\n";
                       
            }

            print "=====================================================\n";

    }

Regards

dmazzini
GSM/UMTS System and Telecomm Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top