如何使用perl模块XML :: LibXML创建xml内容/ doc

问题描述 投票:0回答:1

如何使用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>
perl xml-parsing
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.