Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I think that my brain has reached its capacity and I need help for a algorithm I am not able to implement. It is very important!
I have to parse a xml file (model.xml). In this file, there is a nested repetitive motif. For instance,
I mean in each element (<aaa></aaa>, <bbb></bbb>) I can find the same structure recursivly, without number limitation. (the name of <aaa></aaa> etc. element is random, only the <fixe> has a constant name).
I have done this script which remove the element "fixe"
http://search.cpan.org/~phish/XML-LibXML-1.58/lib/XML/LibXML/Node.pod
Any help would be very appreciated.
I think that my brain has reached its capacity and I need help for a algorithm I am not able to implement. It is very important!
I have to parse a xml file (model.xml). In this file, there is a nested repetitive motif. For instance,
<fixe> <aaa></aaa> <bbb></bbb> <bbb></bbb> <ccc></ccc> </fixe>
I mean in each element (<aaa></aaa>, <bbb></bbb>) I can find the same structure recursivly, without number limitation. (the name of <aaa></aaa> etc. element is random, only the <fixe> has a constant name).
I use DOM with the XML::LibXML API. The goal of my script is to remove the <fixe></fixe> elements and according to parameters (A number od dupplication and name of element) to dupplicate the node. for example, name = eee and number = 4, the xml would become.<fixe> <aaa> <fixe> <eee/> <bbb/> <bbb/> <ccc> <fixe> <aaa/> <eee/> <bbb/> <fff/> <ccc/> </fixe> </ccc> </fixe> </aaa> <bbb/> <bbb/> <ccc/> </fixe><br />
<aaa> <eee/> <eee/> <eee/> <eee/> <bbb/> <bbb/> <ccc> <aaa/> <eee/> <eee/> <eee/> <eee/> <bbb/> <fff/> <ccc/> </ccc> </aaa> <bbb/> <bbb/> <ccc/>
I have done this script which remove the element "fixe"
for the duplication, I have tried to use the addSibling function with a foreach but I got an error, I try also the cloneNode and insertBefore function but no way...#!/usr/bin/perl -w use strict; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("model.xml") or die "cannot open xml fil +e: $!"; my $node = $doc->getDocumentElement(); my @dc = $child[0]->getElementsByTagName('descriptor'); foreach my $descr (@dc) { foreach my $elt ($descr->childNodes()) { next unless $elt->nodeType() == 1; #to avoid text + nodes $descr->addSibling($elt); } my $parent = $descr->parentNode; $parent->removeChild($descr); } print $child[0]->serialize;
http://search.cpan.org/~phish/XML-LibXML-1.58/lib/XML/LibXML/Node.pod
Any help would be very appreciated.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Manipulate xml with libxml
by BaldPenguin (Friar) on Jun 07, 2005 at 18:06 UTC | |
by Anonymous Monk on Jun 08, 2005 at 09:20 UTC | |
by BaldPenguin (Friar) on Jun 08, 2005 at 16:24 UTC | |
by mirod (Canon) on Jun 08, 2005 at 16:34 UTC | |
by Anonymous Monk on Jun 08, 2005 at 12:13 UTC | |
Re: Manipulate xml with libxml
by goonfest (Sexton) on Jun 07, 2005 at 19:59 UTC | |
by Anonymous Monk on Jun 08, 2005 at 09:23 UTC |
Back to
Seekers of Perl Wisdom