如何使用XML :: LibXML在perl中生成以下xml内容
<?xml version="1.0" encoding="UTF-8"?>
<response status="Not Found">
<errorcode>500</errorcode>
<message>some message</message>
<description>some description</description>
</response>
答案 0 :(得分:0)
类似的东西
use XML::LibXML;
my $doc = XML::LibXML->createDocument( "1.0", "UTF-8" );
my $root = $doc->createElement("response");
$root->setAttribute('status'=> 'Not Found');
my %tags = (
errorcode => 500,
message => 'some message',
description => 'some description',
);
for my $name (keys %tags) {
my $tag = $doc->createElement($name);
my $value = $tags{$name};
$tag->appendTextNode($value);
$root->appendChild($tag);
}
$doc->setDocumentElement($root);
print $doc->toString(2); # <= XML indentation