Re: Writing to XML using XML::Writer

2006-08-10 Thread Rob Dixon

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 ?
>>>
>>>   
>>>   
>>> Tom
>>> Aug 2006
>>>   
>>>   
>>> Dicken
>>> Aug 2006
>>>   
>>>   
>>>
>>>   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  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  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



  
Tom
Aug 2006
  
  
Dicken
Aug 2006
  
  
Harriet
Feb 2006
  





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Writing to XML using XML::Writer

2006-08-10 Thread Nath, Alok (STSD)
Hi Robin,
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.
Thanx,
Alok



 

-Original Message-
From: Robin Norwood [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 09, 2006 8:23 PM
To: Nath, Alok (STSD)
Cc: beginners@perl.org
Subject: Re: Writing to XML using XML::Writer

"Nath, Alok (STSD)" <[EMAIL PROTECTED]> writes:

> Hi,
>   Can anybody give me a simple code snippet which writes or
modifies 
> this xml ?
>   
>   
>   
>   Tom
> Aug 2006
>   
>   
>   Dicken
> Aug 2006  
>   
> 
>
>   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.

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.

-RN

> 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

--
Robin Norwood
Red Hat, Inc.

"The Sage does nothing, yet nothing remains undone."
-Lao Tzu, Te Tao Ching

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Writing to XML using XML::Writer

2006-08-09 Thread Robin Norwood
"Nath, Alok (STSD)" <[EMAIL PROTECTED]> writes:

> Hi,
>   Can anybody give me a simple code snippet which writes or
> modifies this xml ?
>   
>   
>   
>   Tom
> Aug 2006
>   
>   
>   Dicken
> Aug 2006  
>   
> 
>
>   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.

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.

-RN

> 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

-- 
Robin Norwood
Red Hat, Inc.

"The Sage does nothing, yet nothing remains undone."
-Lao Tzu, Te Tao Ching

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]