Home > Archive > PERL Modules > January 2006 > Using XML::Twig
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I'm a little 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>
| |
| metaperl@gmail.com 2006-01-10, 3:59 am |
| 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
| |
|
|
|
|
| Randal L. Schwartz 2006-01-10, 3:59 am |
| >>>>> "Rich" == Rich <rjkoop@gmail.com> writes:
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
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
| |
|
| Here's the solution I got from mirod on www.perlmonks.org from...
in reply to XML::Twig question
I think the $twig->purge is a little too strong for what you are doing.
It seems to interfere with the way the twig is stored. Using
$element->delete instead when the element is not one you want to output
would work better.
I will investigate some more to understand better what's going on, but
in the mean time a handler like this seems to work:
sub parseEntry
{ my ($twig,$element)=@_;
my $id=$element->att("id");
if ($id eq "OVAL3")
{ $twig->flush();
return 1;
}
else
{ $element->delete; }
return 0;
}
| |
|
|
| Darin McBride 2006-01-10, 3:59 am |
| Rich wrote:
> Here's the solution I got from mirod on www.perlmonks.org from...
Please ignore Randal. He can be a bit harsh. In less harsh language, I
think this is what you can take from this:
a) when cross-posting between newsgroups and perlmonks (or most web forums),
it is considered common courtesy (well, I'm not sure how common it is - but
it's still courtesy) to alert people as to where your original question
is/was. Since you seem to be using google already for newsgroup access,
you could easily have posted your original question as a URL on perlmonks
(in addition to copying it there):
Monks,
In [http://...|this newsgroup article], I asked the following question:
...
(fill in the "..." appropriately) This would have many side effects, the
least of which would be keeping Randal happy. More importantly, it would
show people the effort you're going through, and alert people who read both
to know that they only need to respond to one because you're the same
person in both places. It also tells people about other venues for posting
questions if they weren't aware of this newsgroup - it's kinda like subtle
advertising. ;-)
b) Having cross-posted, you should return to your other questions to post a
URL to the answer. Rather than reposting mirod's response, just post the
URL:
I posted this question to perlmonks here:
http://www.perlmonks.org/index.pl?node_id=518054
and mirod responded with this answer which is working perfectly:
http://www.perlmonks.org/index.pl?node_id=518089
Thanks, all!
This has many similar side effects: it allows people who do a google search
that come across your question to see the answer, and then they can search
perlmonks for other answers as well, and it provides some subtle
advertising for the site where you actually got your response. In this
case, with the link to mirod's answer is a link to mirod's user information
on perlmonks where people with XML::Twig questions would find further links
for further information. You can easily spend days following these links
for interesting stuff (trust me ;-}) - and through that simple update
message as I typed above, you will enable many people to learn much more
about this topic, and waste millions of dollars of employer's time. ;-)
I see nothing wrong with cross-posting. A little courtesy, however, is
appreciated. (In both directions - harshness is a discourteous response to
such a minor infraction.)
| |
| John Bokma 2006-01-10, 3:59 am |
| Darin McBride <dmcbride@tower.to.org.no.spam.for.me> wrote:
> Rich wrote:
>
>
> Please ignore Randal.
I'd rather not :-D
> He can be a bit harsh.
Odd, never noticed :-D.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)
| |
| Richard Koop 2006-01-10, 3:59 am |
| I'm almost worried to post another question... I'll get my lawyer to
check over it before I post!
| |
|
|
|
|
|