×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

XML::Lib - Getting values from XML Node

XML::Lib - Getting values from XML Node

XML::Lib - Getting values from XML Node

(OP)
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

RE: XML::Lib - Getting values from XML Node

(OP)
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

RE: XML::Lib - Getting values from XML Node

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close