2

This thread is in continuation of Perl script to populate an XML file.

The file I want to change is:

<?xml version="1.0" encoding="UTF-8"?>
  <configuration start="earth">
    <country-list>
      <country name="japan">
        <description></description>
        <start>1900</start>
        <end/>
      </country>
      <country name="italy">
        <description></description>
        <start>1950</start>
        <end/>
      </country>
      <country name="korea">
        <description></description>
        <start>1800</start>
        <end/>
      </country>
    </country-list>
  </configuration>

I want to add a new country here in this list.

In previous question, Perl script to populate an XML file.

#Get the list of cities as a list, then push "Tokyo" to it.
push @{$doc->{countries}->{country}->{'japan-'}->{city}}, 'Tokyo';

This was suggested to add a new tag, but in my case not sure how exactly can I use "push". I am not able to map to the correct tag.

Community
  • 1
  • 1
hari
  • 9,439
  • 27
  • 76
  • 110
  • push @{$doc->{configuration}->{'country-list'}->{country}}, $platform_name ; -- was suggested but I am not sure, how exactly should I use push to add a new country. – hari Nov 29 '10 at 07:28

2 Answers2

2

I find XML::DOM a lot simpler to use. It may be a bit verbose, but you can easily understand what it is doing.

use XML::DOM;

#parse the file
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ("test.xml");
my $root = $doc->getDocumentElement();

#get the country-list element
my $countryListElement = pop(@{$root->getElementsByTagName('country-list')}); 

#create a new country element
my $newCountryElement= $doc->createElement('country');
$newCountryElement->setAttribute("name","England");

my $descElement= $doc->createElement('description');
$newCountryElement->appendChild($descElement);

my $startElement= $doc->createElement('start');
my $startTextNode= $doc->createTextNode('1900');
$startElement->appendChild($startTextNode);
$newCountryElement->appendChild($startElement);

my $endElement= $doc->createElement('end');
$newCountryElement->appendChild($endElement);

#add the country to the country-list
$countryListElement->appendChild($newCountryElement);

#print it out
print $doc->toString;

#print to file
$doc->printToFile("out.xml");
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Wow, works great and easy to understand :) - thanks much. Only issue I have is, when I add a node, the o/p looks kinda weird, the indentation and spacing is awkward. Any solution to that? – hari Nov 29 '10 at 18:59
  • Aah, that was the "\n" chars being added with my user input field. "chomp" did the trick. Thanks Dogbane. – hari Nov 29 '10 at 19:44
0

You can't use push. Push is for appending an item to an array (a list). Judging by the "push" command somebody gave you before, countries are represented as a hash, not a list, so you need something like

$doc->{countries)->{country}->{Transylvania} = {};

That is creating an empty hash for 'Transylvania'. Your system may require there to be some structure in there.

Colin Fine
  • 3,334
  • 1
  • 20
  • 32