how to $doc->createElement with XML::LibXML

Hi,

i am trying to create a xml file with the following

<?xml version="1.0" encoding="UTF-8"?>
<file name="file 1"/>
<file name="file 2"/>

i try the following code without any success, can someone help me to
understand what i am dong wrong?

use strict;
use XML::LibXML;

my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
my $root = $doc->createElementNS('', 'file');
my $attr = $doc->createAttributeNS( "", "name", "file 1" );
$root->setAttributeNodeNS( $attr );
$doc->setDocumentElement($root);
print "\n\n ";
print $doc->toString(2);
print "\n\n ";

my $root2 = $doc->createElementNS('', 'file');
my $attr2 = $doc->createAttributeNS( "", "name", "file 2" );
$root2->setAttributeNodeNS( $attr2 );
$doc->setDocumentElement($root2);
print "\n\n ";
print $doc->toString(2);
print "\n\n ";

the result is

<?xml version="1.0" encoding="UTF-8"?>
<file name="file 1"/>


<?xml version="1.0" encoding="UTF-8"?>
<file name="file 2"/>

Regards Zacky


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
zacky az [ Mo, 22 Februar 2010 20:54 ] [ ID #2033070 ]

Re: how to $doc->createElement with XML::LibXML

Hi.

On Monday 22 Feb 2010 21:54:21 zacky az wrote:
> Hi,
>
> i am trying to create a xml file with the following
>
> <?xml version="1.0" encoding="UTF-8"?>
> <file name="file 1"/>
> <file name="file 2"/>

This is invalid XML - it has two top-level elements and XML must have exactly
one. So it cannot be done. If you want you can do this:

{{{
<?xml version="1.0" encoding="UTF-8"?>
<files>
<file name="file 1"/>
<file name="file 2"/>
</files>
}}}

Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mo, 22 Februar 2010 23:16 ] [ ID #2033071 ]

Re: how to $doc->createElement with XML::LibXML

On Feb 23, 12:16=A0am, shlo... [at] iglu.org.il (Shlomi Fish) wrote:
> Hi.
>
> On Monday 22 Feb 2010 21:54:21 zacky az wrote:
>
> > Hi,
>
> > i am trying to create a xml file with the following
>
> > =A0<?xml version=3D"1.0" encoding=3D"UTF-8"?>
> > <file name=3D"file 1"/>
> > =A0<file name=3D"file 2"/>
>
> This is invalid XML - it has two top-level elements and XML must have exa=
ctly
> one. So it cannot be done. If you want you can do this:
>
> {{{
> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
> <files>
> <file name=3D"file 1"/>
> <file name=3D"file 2"/>
> </files>
>
> }}}
>
> Regards,
>
> =A0 =A0 =A0 =A0 Shlomi Fish
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish =A0 =A0 =A0http://www.shlomifish.org/
> First stop for Perl beginners -http://perl-begin.org/
>
> Deletionists delete Wikipedia articles that they consider lame.
> Chuck Norris deletes deletionists whom he considers lame.
>
> Please reply to list if it's a mailing list post -http://shlom.in/reply.

Thank, this was my problem

Regards zacky


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
zacky az [ Di, 23 Februar 2010 05:40 ] [ ID #2033190 ]
Perl » gmane.comp.lang.perl.beginners » how to $doc->createElement with XML::LibXML

Vorheriges Thema: file handling
Nächstes Thema: being smart about script structure