Click here to get back home

Using XML::Twig

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Using XML::Twig Rich 12-19-2005
---> Re: Using XML::Twig metaperl@gmail....12-20-2005
Posted by Rich on December 19, 2005, 4:59 pm
Please log in for more thread options


I'm a little confused using the Perl XML::Twig module. What I'm trying
to do is output a subset of the matched twig_root entries in an XML
file. Essentially I call a method for the 'definition' twig_root and
then I only want to output some of the definitions based on a
condition. Of course I also want the root of the XML document to be
output. It isn't. What am I doing wrong? I expect the <oval> tag and
the selected <definition> and nothing else. I'm using the root <oval>
tag.

XML
===
<?xml version="1.0" encoding="UTF-8"?>
<oval xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval#redhat
redhat-schema.xsd http://oval.mitre.org/XMLSchema/oval#windows
windows-schema.xsd http://oval.mitre.org/XMLSchema/oval#unix
unix-schema.xsd http://oval.mitre.org/XMLSchema/oval#independent
independent-schema.xsd http://oval.mitre.org/XMLSchema/oval#solaris
solaris-schema.xsd http://oval.mitre.org/XMLSchema/oval
oval-schema.xsd" xmlns:oval="http://oval.mitre.org/XMLSchema/oval"
xmlns="http://oval.mitre.org/XMLSchema/oval"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:redhat="http://oval.mitre.org/XMLSchema/oval#redhat"
xmlns:windows="http://oval.mitre.org/XMLSchema/oval#windows"
xmlns:solaris="http://oval.mitre.org/XMLSchema/oval#solaris">
<generator>
<schema_version>4.2</schema_version>
<timestamp>20051212102623</timestamp>
</generator>
<definitions>
<definition id="OVAL2" class="vulnerability">
<affected family="redhat">
<redhat:platform>Red Hat Linux 9</redhat:platform>
<product>Mutt</product>
</affected>
</definition>
<definition id="OVAL3" class="vulnerability">
<affected family="windows">
<windows:platform>Microsoft Windows 2000</windows:platform>
<windows:platform>Microsoft Windows Server
2003</windows:platform>
<product>Microsoft Exchange Server 2003</product>
</affected>
</definition>
<definition id="OVAL6" class="vulnerability">
</definition>
</definitions>
<crap>This is some crap</crap>
</oval>

Perl code to extract a subset of the definitions
==================================
use XML::Twig;

my $doc=new XML::Twig
        (
                twig_roots =>
                {
                        "definition" => \&parseEntry
                }
        );

sub parseEntry
{
        my ($twig,$element)=@_;

        my $id=$element->att("id");
        if ($id eq "OVAL3")
        {
                $element->flush();
                return 1;
        }

        $twig->purge();
        return 0;
}

$doc->parsefile("test.xml");
$doc->flush();


The output
========
<definition class="vulnerability" id="OVAL3">
<affected family="windows">
<windows:platform>Microsoft Windows 2000</windows:platform>
<windows:platform>Microsoft Windows Server
2003</windows:platform>
<product>Microsoft Exchange Server 2003</product>
</affected>
</definition>
</oval>



Posted by metaperl@gmail.com on December 20, 2005, 5:43 am
Please log in for more thread options


I emailed the author of XML::Twig and he should help you shortly. But,
he visits perlmonks.org regularly and you can email him via his
contact info at http://www.xmltwig.com



Posted by Rich on December 20, 2005, 6:46 pm
Please log in for more thread options


Thanks. Got the reply at www.perlmonks.org and it worked great!



Posted by John Bokma on December 21, 2005, 2:57 am
Please log in for more thread options



> Thanks. Got the reply at www.perlmonks.org and it worked great!

[a] quote
[b] it's nice to post a link to the solution

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)



Posted by Randal L. Schwartz on December 21, 2005, 6:42 am
Please log in for more thread options



Rich> Thanks. Got the reply at www.perlmonks.org and it worked great!

Which means you're evil, for having posted it multiple places without
disclosure.

Bad on you.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


Similar ThreadsPosted
XML Twig help April 8, 2005, 2:18 pm
XML::Twig June 9, 2006, 1:50 pm
XML::Twig June 9, 2006, 6:27 pm
XML::Twig question December 23, 2007, 2:25 pm
Help with XML::Twig xpath syntax, please October 16, 2005, 6:56 pm
XML::Twig parseurl with input Headers/XML January 16, 2005, 10:58 pm
XML::Twig::XPath - strange problem November 10, 2005, 9:44 am
XML::Twig produces double encoded UTF-8 January 4, 2007, 3:36 pm
XML::Twig constructor disregarding map_xmlns - bug in module? August 29, 2004, 1:21 pm
Problem Parsing Huge XML file using XML::Twig April 23, 2007, 10:24 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap