2

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 Answer 1

2

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 :-)

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.