tags:

views:

256

answers:

1

How can I get actual attribute value instead of XML::DOM::NamedNodeMap=HASH(0xa3246d4) while using getAttribute function from XML::DOM parser

Code

 my $parent = $doc->getElementsByTagName ("ParentTag")->item(0);
         my @parent = $childnodes->getChildNodes();
         {
           foreach  my $parent(@parent) 
            {
             if ($parent->getNodeType == ELEMENT_NODE)
               {
                 print $parent->getNodeName;
                 print $parent->getAttributes;
               }
            }
         }
+1  A: 

The return value of getAttributes looks to be an XML::DOM::NamedNodeMap object, so you can use that object to get attribute values by name, e.g.

my $nodemap = $parent->getAttributes;
my $node = $nodemap->getNamedItem('foo');

$node, in turn will be an XML::DOM::Node object, which will have its own methods and will have its own documentation.

There are a lot of classes and reading through all their [docs] will help. If it seems like too much, you might be able to make use of XML::Simple, which is usually good enough until its no longer good enough :-)

jsoverson
Thank you jsoverson for your response. I was thinking of XML::Simple but somehow decided on XML::DOM as I have used DOM Parser Earlier.
Rachel
XML::DOM is not really maintained these days. Did you try XML::LibXML? It's probably a better option these days if you want to use the DOM.
mirod
I have not tried XML::LibXML but I would surely try it but recently it has been decided to use SAX parser and I guess for SAX Parsing in Perl we have XML::SAX::Intro and XML::Parser::PerlSAX
Rachel