Hello dear Monks, I`m kinda new to perl and xml, but anyway, here is my problem: 1. I have a xml file (url_config.xml) like this:
<?xml version="1.0"?> <urls> <url id="1" link="http://localhost/index.php?page=news&amp;id_news=1 +" name="First Test Name" /> <url id="2" link="http://localhost/index.php?page=news&amp;id_news=1 +7" name="2-nd test name with 2 as id" /> <url id="4" link="http://localhost/index.php?page=news&amp;id_news=5 +" name="another cool link" /> </urls>
2. using perl and XML::DOM i want to add a new line to the url_config.xml file, in the end this file should look like this:
<?xml version="1.0"?> <urls> <url id="1" link="http://localhost/index.php?page=news&amp;id_news=1 +" name="First Test Name" /> <url id="2" link="http://localhost/index.php?page=news&amp;id_news=1 +7" name="2-nd test name with 2 as id" /> <url id="4" link="http://localhost/index.php?page=news&amp;id_news=5 +" name="another cool link" /> <url id="5" link="http://localhost/index.php?page=news&amp;id_news=2 +3" name="another cool link added later on" /> </urls>
3. I know how to read the xml file, I know how to parse the file but i don`t know how to insert a new row.
#!/usr/bin/perl -w use XML::DOM; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ("url_config.xml"); my $nodes = $doc->getElementsByTagName( "url" ); my $entries = $nodes->getLength; my @id_array; # insert all the id`s in a array: for (my $i = 0; $i < $entries; $i++) { my $node = $nodes->item ($i); my $href = $node->getAttributeNode ("id"); print $href->getValue . "\n"; push(@id_array, $href->getValue); } # last id: my $last_id = pop(@id_array); print $last_id; print "\n"; print "total no. of records::" . $entries . "\n";

In reply to XML::DOM -> writing by bigallow

Title:
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, details, 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, summary, 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.