0

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?

2
  • 6
    Also, you should start using strict and warnings. You didn't have strict in your last question as well. Commented Feb 22, 2016 at 11:40
  • 1
    @simbabque Thanks for the tip. I took your advice (and added a lot of variable declarations). Commented Feb 22, 2016 at 12:57

1 Answer 1

3

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

$doc->setDocumentElement($root);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.