为什么用XML :: LibXML生成后我的XML文件为空 [英] Why is my XML file empty after generating with XML::LibXML

查看:139
本文介绍了为什么用XML :: LibXML生成后我的XML文件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的perl脚本

#!/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');

但是,输出文件test.xml仅包含以下行:

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

<?xml version="1.0"?>

我认为这意味着已创建xml文件,但未添加任何内容.我没有任何错误,所以我不知道在哪里看.我在做什么错了?

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?

推荐答案

您需要将创建的元素添加到文档中:

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

$doc->setDocumentElement($root);

这篇关于为什么用XML :: LibXML生成后我的XML文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆