Hi Lawrence,

  Works great, thanks! Never thought of appending the comment directly to the 
$doc, just assumed I
Needed to set the root first.

Regards,

John

From: Lawrence Statton <lawre...@cluon.com>
Sent: Monday, September 10, 2018 9:44 PM
To: beginners@perl.org
Subject: Re: XML::LibXML and comments




On Sep 10, 2018, at 6:33 AM, John Cortland Morgan 
<johncortland.mor...@ericsson.com<mailto:johncortland.mor...@ericsson.com>> 
wrote:

Hi,

 I'm trying to place a comment directly after the XML declaration using 
XML::LibXML,
But cannot seem to manage, always receiving error:

setDocumentElement: ELEMENT node required at .../LibXML.pm line 1393

What I would like:

<?xml version="1.0" encoding="utf-8">
<!-- test comment -->
<products>
 <field>testing</field>
</products>

My relevant code thus far:

My $dom = XML::LibXML::Document->new( "1.0", "UTF-8");
My $root =  XML::LibXML::Comment->new( "test comment" );
$dom->setDocumentElement($root);


Any help would be greatly appreciated. I'm kinda stuck with using XML::LibXML 
though.

John

--
To unsubscribe, e-mail: 
beginners-unsubscr...@perl.org<mailto:beginners-unsubscr...@perl.org>
For additional commands, e-mail: 
beginners-h...@perl.org<mailto:beginners-h...@perl.org>
http://learn.perl.org/


The root of a document cannot be a comment, however you can add a comment with

$dom->addChild($dom->createComment(‘test comment’))

#!/usr/bin/perl

use strict;
use warnings;
use XML::LibXML;

my $doc = XML::LibXML::Document->new(qw/1.0 utf-8/);

$doc->appendChild($doc->createComment('test comment'));
$doc->setDocumentElement(my $e_products = $doc->createElement('products'));
$e_products->appendTextChild(field => 'testing');

print $doc->toString(1);


Reply via email to