The easiest way to extract information from XML documents with XML::LibXML
is to use the find
family of methods. These methods use an XPath expression to select nodes and values from the document. The following script extracts the data you need:
use XML::LibXML;
my $doc = XML::LibXML->load_xml(location => 'so.xml');
for my $entry ($doc->findnodes('//entry')) {
my $name = $entry->getAttribute('name');
my $role = $entry->findvalue(
'local-name(permissions/role-based/*[.="yes"])'
);
print("$name;$role\n");
}
It prints
asd;superuser
fgh;superuser
jkl;superreader
I used the local-name
XPath function to get the name of the role element.
Note that you might want to use Text::CSV
to create CSV files in a more robust way.