Trying to print XML elements but only one prints when there should be multiple. I'm trying to print the node name and all of it's extendedAttributes. Here is a sample of the input & output file and the script I‘m using.

########## Output
172.30.1.7
NA_ID
26401

jfllt000fi01.atoesa.com
Device Category
Server

#!/usr/bin/perl -w use XML::LibXML; use strict; use warnings; my $filename = 'topo-small-sample.xml'; ## NNM XML Topology Source + File my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); foreach my $node($doc->findnodes('/topo/node')){ my ($nodename) = $node->findnodes('./name'); my ($caname) = $node->findnodes('./extendedAttributes/attribut +e'); print $nodename->to_literal, "\n", "\t", $caname->to_literal, +"\t", "\n" } ########## Source file example <?xml version="1.0" encoding="UTF-8"?> <topo> <node> <shortname>10B_Gate_W</shortname> <name>172.30.1.7</name> <deviceDescription>Two Network Module Slots, Four WIC slots, T +wo Gigabit Ethernet ports 3800 family router</deviceDescription> <deviceCategory>Router</deviceCategory> <deviceFamily>Cisco 3800 Series Integrated Services Routers</d +eviceFamily> <deviceVendor>Cisco</deviceVendor> <capabilities> <capability>com.hp.nnm.capability.card.cisco.entfructrl</c +apability> <capability>com.hp.ov.nnm.capability.metric.cisco.env.fan< +/capability> <capability>com.hp.nnm.capability.metric.cpm2</capability> </capabilities> <extendedAttributes> <attribute> <name>NA_ID</name> <value>26401</value> </attribute> <attribute> <name>Device Category</name> <value>Router</value> </attribute> </extendedAttributes> </node> <node> <shortname>jfllt000fi01</shortname> <name>jfllt000fi01.atoesa.com</name> <deviceDescription>Microsoft Windows NT Server</deviceDescript +ion> <deviceCategory>Server</deviceCategory> <deviceFamily>Microsoft Windows NT Servers</deviceFamily> <deviceVendor>Microsoft</deviceVendor> <capabilities> <capability>com.hp.nnm.capability.metric.hr</capability> <capability>com.hp.nnm.capability.metric.ietf.DISK_HOST-RE +SOURCES-MIB</capability> </capabilities> <extendedAttributes> <attribute> <name>Device Category</name> <value>Server</value> </attribute> </extendedAttributes> </node> </topo>

In reply to XML Output by bitretriever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.