From: zacky az on
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
From: Ben Morrow on

Quoth zacky az <zacky.azoulay(a)gmail.com>:
>
> 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 not valid XML. An XML document has a *single* root element
containing all the other elements; this has two <file> elements at the
root.

What makes you think you need the XML to look like this?

Ben

From: zacky az on
On Feb 22, 10:59 pm, Ben Morrow <b...(a)morrow.me.uk> wrote:
> Quoth zacky az <zacky.azou...(a)gmail.com>:
>
>
>
> > 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 not valid XML. An XML document has a *single* root element
> containing all the other elements; this has two <file> elements at the
> root.
>
> What makes you think you need the XML to look like this?
>
> Ben

Hi,

Thank, a pmd process is creating an xml like this and i see my problem
the pmd in the root

<?xml version="1.0" encoding="UTF-8"?>
<pmd version="4.2.5" timestamp="2010-02-22T18:24:22.007">
<file name="ContractUploadProcessor.java">
<violation beginline="3" endline="3" begincolumn="1" endcolumn="20"
rule="UnusedImports" ruleset="Import Statement Rules"
package="processors" externalInfoUrl="http://pmd.sourceforge.net/rules/
imports.html#UnusedImports" priority="4">
Avoid unused imports such as 'java.io.File'
</violation>
<file name="ContractCsvEngine.java">
<violation beginline="26" endline="26" begincolumn="1" endcolumn="54"
rule="UnusedImports" ruleset="Import Statement Rules"
package="imports" externalInfoUrl="http://pmd.sourceforge.net/rules/
imports.html#UnusedImports" priority="4">
Avoid unused imports such as 'ataImportActionBo'
</violation>
</file>
</pmd>

thank and Regards Zacky
From: sln on
On Mon, 22 Feb 2010 12:43:31 -0800 (PST), zacky az <zacky.azoulay(a)gmail.com> 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"/>
>
>i try the following code without any success, can someone help me to
>understand what i am dong wrong?

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

The results are correct at the time of printing.
One element is allowed at root, and no content at root.
Thats why its called 'ROOT' level.

I just tried this for the first time (ie: dom tree), but
it looks like its not condusive to creating a dom from scratch,
more like its good for editing a tree.

From below, it appears you have to be comfortable with objects,
and creating a few helper subs might help a lot. The docs seem
very thin (I just looked at it enough to do the below stuff).

Good luck!
-sln

------------------
use strict;
use warnings;

use XML::LibXML;

# Create document and ROOT
my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
my $root = $doc->createElement('root');
$doc->setDocumentElement( $root );

my $file = $doc->createElement('file');
my $file2 = $doc->createElement('file2');

# Add a 'file' child node under ROOT
my $firstchild = $root->addChild( $file );

# Add a 'name=' attribute to the 'file' clone
$firstchild->setAttributeNode( $doc->createAttribute( "name", "file 1" ) );

# Clone 'file' node, add a 'location=' attribute
my $fileclone = $root->addChild( $file->cloneNode() );
$fileclone->setAttributeNode( $doc->createAttribute( "location", "lost" ) );

# Add a 'file2' child node under ROOT, with a 'name=' attribute
$root->addChild( $doc->createElement('file2') )->
setAttributeNode( $doc->createAttribute( "name", "file 2" ) );

# Add a child node under the first 'file' node under ROOT
$firstchild->addChild( $doc->createElement('child') );

print "\n\n ";
print $doc->toString(2);
print "\n\n ";

__END__

<?xml version="1.0" encoding="UTF-8"?>
<root>
<file name="file 1">
<child/>
</file>
<file name="file 1" location="lost"/>
<file2 name="file 2"/>
</root>