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

假如想象 提交于 2019-12-25 07:06:18

问题


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?


回答1:


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

$doc->setDocumentElement($root);


来源:https://stackoverflow.com/questions/35552520/why-is-my-xml-file-empty-after-generating-with-xmllibxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!