I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2012-06-20 13:57:37.396489-07 by b0sque in response to 13661
Re: Passing XML twig element to another subroutine
So, sorry it's been about a while, got put on a different project at work. So, I've fixed all the problems in my code, and it works like a charm, except for this same issue. This time, I'll explain what I'm needing, and put in some real XML so it makes sense.
Basically, I'm getting XML report files from a tool. I'm normalizing them for import into a database. The issue is a) the XML tags in the output of the tool suck, and b) some machines have multiple IP addresses....
Below is some example XML: (it's example data; i realize there is no 127.0.0.2)
<ASSET> <ASSET_ID TYPE="Host Name">System A</ASSET_ID> <ASSET_ID TYPE="IP Address">127.0.0.1</ASSET_ID> <ASSET_ID TYPE="IP Address">127.0.0.2</ASSET_ID> <ASSET_ID TYPE="MAC Address">00:00:00:00:00:00</ASSET_ID> <test> <value>blah</value> </test> <test> <value>blah</value> </test> </ASSET>

That's the part of the XML I'm struggling with. So, how do I query this out? If I set the twig_root like this:
my $roots = { 'ASSET/ASSET_ID' => 1 }; my $twig = new XML::Twig(TwigRoots => $roots); $twig->parsefile($scan_file); my $root = $twig->root;

Now, if it's coded like the above, I can get some of the values, but I can't figure out how to get specific ones. Heck, if I could just loop through the children, and check the attrib value each time and concat it to a variable, that would be awesome. Is there a "next_child" method I don't know about???
Here's what I'm able to do right now:
$hostname = $root->first_child->text; $mac = $root->last_child->text;

so, I can get the first child text value for hostname, and the last_child value and grab the MAC, but I can't figure out how to get at the child with the IP Address... The problem is if I try "first_child_matches", all of them will be the same since they're all <ASSET_ID> tags. I don't know of any "next_child" or "next_child_matches" methods.
Is there a better way to loop through this? If I can get a loop working, it'd be easy to write the if statement to check the attrib and the concat the text value to the correct variable (ie, $ip).
Thoughts anyone?
Direct Responses: Write a response