Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to $doc->createElement with XML::LibXML

Reply
Thread Tools

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

 
 
zacky az
Guest
Posts: n/a
 
      02-22-2010
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:ocument->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
 
Reply With Quote
 
 
 
 
zacky az
Guest
Posts: n/a
 
      02-22-2010
On Feb 22, 10:59*pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth zacky az <(E-Mail Removed)>:
>
>
>
> > 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
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      02-23-2010
On Mon, 22 Feb 2010 12:43:31 -0800 (PST), zacky az <(E-Mail Removed)> 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:ocument->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>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments