Search code examples
xmlperlxml-libxml

Why is my XML file empty after generating with XML::LibXML


This is my perl script

#!/usr/bin/perl

use XML::LibXML;
$doc = XML::LibXML::Document->new;

my $root = $doc->createElement("log");
$root -> setAttribute("time" => "now");

my $tag = $doc->createElement("greeting");

my $value = "hello";
$tag -> appendTextNode($value);
$root -> appendChild($tag);

$doc -> toFile('test.xml');

However, the output file test.xml only consists of this line:

<?xml version="1.0"?>

I assume that this means that the xml file is created but nothing is added to it. I don't get any errors though, so I don't know where to look. What am I doing wrong?


Solution

  • You need to add the element you created to the document:

    $doc->setDocumentElement($root);