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

Z

zacky az

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
 
Z

zacky az

Quoth zacky az <[email protected]>:





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
 
S

sln

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top