Nath, Alok (STSD) wrote: > > Robin Norwood wrote: >> >> Nath, Alok (STSD) wrote: >> >>> Hi, >>> Can anybody give me a simple code snippet which writes or >>> modifies this xml ? >>> >>> <my_list> >>> <guy name="SomeGuy"> >>> <user>Tom</user> >>> <date-of-birth>Aug 2006</date-of-birth> >>> </guy> >>> <guy name="AnotherGuy"> >>> <user>Dicken</user> >>> <date-of-birth>Aug 2006</date-of-birth> >>> </guy> >>> </my_list> >>> >>> I want to read and write into this simple xml, basically >>> changing the values or deleting them. >>> After scanning through different XML modules I zeroed into >>> XML::SimpleObject for reading.For >>> reading I know how to do it.For writing I am struggling.I want >>> to use XML::Writer. >>> >>> Any help greatly appreciated. >>> Thanx, >>> Alok >>> >>> use XML::Parser ; >>> use XML::SimpleObject; >>> use XML::Writer; >>> use IO::File; >>> >>> my $xml = "Inventory.xml" ; >>> >>> my $parser = XML::Parser->new(ErrorContext => 2, Style => "Tree"); >>> my $xso = XML::SimpleObject->new( $parser->parsefile($xml) ); >>> >>> # Reading an XML file >>> foreach my $element ($xso->child("my_list")->children("guy")) >>> { >>> print " User: " . $element->child("user")->value . "\n"; >>> >>> print " Date of Birth: " . >>> $element->child("date-of-birth")->value . "\n"; >>> >>> print "\n" ; >>> } >>> >>> # Code for Writing into the above xml >> >> Well...have you looked at perldoc XML::Writer ? The module is pretty >> well documented...if you start with the hello world example in the docs, >> you should be able to get to your example with a few minutes of hacking. >> >> If you have any problems with it, don't hesitate to ask. > > yeah I have looked at documentation of XML::Writer. > The example explains how to create a new xml file > and insert tags there, but does not says how to modify > already existing XML. > > My problem is I want to read and write into the same > xml file.For reader I am using XML::SimpleObject and > and for writing XML::Writer. > > Both of them uses separate objects. > I want to pass the XML(used by XML::SimpleObject) to the writer > object for modification, so that I can do some write operation. > > Any pointers also is fine.
Hi Alok Now that you've posted this I understand what you're trying to do. You seemed to want some code that parsed an XML file and then rebuilt the same XML for output. Now I see that you want to do something like insert new <guy> elements into an existing XML file. I don't believe you should use XML::Writer. It forces you to explicitly output start and end tags and all the stuff in between just as if you were using print(), except that it complains if what you have output isn't valid XML. It may seem daunting to approach something like XML::Twig or XML::DOM, but please make the effort as it will simplify your life enormously in the end. Here, for example, is a solution for you using XML::Twig, which adds another guy with a name of NewGuy, a user of Harriet, and a date of birth of Feb 2006. Bear in mind that I'm still guessing what you want to do with this file, but hopefully you can extrapolate from what I've written or come back to the list for further help. By the way, please bottom-post your reply as it makes threads of any length much easier to follow. Thanks. This program expects the XML that you posted in a file called Inventory.xml. It adds a new <guy> element and then prints out the result both in the same way as your program and then as well-formed XML. I think it's a lot clearer than your original code with XML::SimpleObject - it's no longer and it does a lot more. HTH, Rob use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(pretty_print => 'indented'); $twig->parsefile('Inventory.xml'); my $guy = XML::Twig::Elt->new( 'guy', {'name' => 'NewGuy'}, XML::Twig::Elt->new('user' => 'Harriet'), XML::Twig::Elt->new('date-of-birth' => 'Feb 2006'), ); $guy->paste(last_child => $twig->root); foreach my $element ($twig->root->children('guy')) { print " User: ", $element->first_child('user')->text, "\n"; print " Date of Birth: ", $element->first_child('date-of-birth')->text, "\n"; print "\n"; } $twig->print; OUTPUT User: Tom Date of Birth: Aug 2006 User: Dicken Date of Birth: Aug 2006 User: Harriet Date of Birth: Feb 2006 <?xml version="1.0"?> <my_list> <guy name="SomeGuy"> <user>Tom</user> <date-of-birth>Aug 2006</date-of-birth> </guy> <guy name="AnotherGuy"> <user>Dicken</user> <date-of-birth>Aug 2006</date-of-birth> </guy> <guy name="NewGuy"> <user>Harriet</user> <date-of-birth>Feb 2006</date-of-birth> </guy> </my_list> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>