bigallow has asked for the wisdom of the Perl Monks concerning the following question:
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&id_news=1 +" name="First Test Name" /> <url id="2" link="http://localhost/index.php?page=news&id_news=1 +7" name="2-nd test name with 2 as id" /> <url id="4" link="http://localhost/index.php?page=news&id_news=5 +" name="another cool link" /> </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.<?xml version="1.0"?> <urls> <url id="1" link="http://localhost/index.php?page=news&id_news=1 +" name="First Test Name" /> <url id="2" link="http://localhost/index.php?page=news&id_news=1 +7" name="2-nd test name with 2 as id" /> <url id="4" link="http://localhost/index.php?page=news&id_news=5 +" name="another cool link" /> <url id="5" link="http://localhost/index.php?page=news&id_news=2 +3" name="another cool link added later on" /> </urls>
#!/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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::DOM -> writing
by bigallow (Initiate) on Jun 02, 2004 at 15:50 UTC |