Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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,

<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).

<fixe> <aaa> <fixe> <eee/> <bbb/> <bbb/> <ccc> <fixe> <aaa/> <eee/> <bbb/> <fff/> <ccc/> </fixe> </ccc> </fixe> </aaa> <bbb/> <bbb/> <ccc/> </fixe><br />
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.

<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"

#!/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;
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...
Any help would be very appreciated.

In reply to Manipulate xml with libxml by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2021-01-19 18:05 GMT
    Find Nodes?
      Voting Booth?