问题
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