如何使用 LibXML 在 Perl 中编写 HTML 标签
How to write HTML tags in Perl using LibXML
我正在使用 LibXML 来 read/write 一个 Android strings.xml 文件。有时,我需要写 html 个元素,例如 <b>
或 <i>
。我试过做这样的事情(例如):
#!/usr/bin/env perl
#
# Create a simple XML document
#
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0', 'utf-8');
my $root = $doc->createElement("resources");
my $tag = $doc->createElement("string");
$tag->setAttribute('name'=>'no_messages');
$tag->appendText("You have <b>no</b> messages");
$root->appendChild($tag);
$doc->setDocumentElement($root);
print $doc->toString();
但我最终得到的是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="no_messages">You have <b>no</b> messages</string>
</resources>
我正在寻找的是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="no_messages">You have <b>no</b> messages</string>
</resources>
由于不支持innerHTML
,您必须手动添加文本和标签:
my $tag = $doc->createElement("string");
$tag->setAttribute('name'=>'no_messages');
$tag->appendText("You have ");
$b = $doc->createElement("b");
$b->appendText("no");
$tag->appendChild("b");
$tag->appendText(" messages");
那个,还是用parser.
XML::LibXML::Element
对象有一个 appendWellBalancedChunk
方法可以完全满足您的要求。
这是一个基于您自己的示例代码的演示
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new(qw/ 1.0 utf-8 /);
my $root = $doc->createElement('resources');
my $tag = $doc->createElement('string');
$tag->setAttribute(name => 'no_messages');
$tag->appendWellBalancedChunk('You have <b>no</b> messages');
$root->appendChild($tag);
$doc->setDocumentElement($root);
print $doc->toString;
输出
<?xml version="1.0" encoding="utf-8"?>
<resources><string name="no_messages">You have <b>no</b> messages</string></resources>
我正在使用 LibXML 来 read/write 一个 Android strings.xml 文件。有时,我需要写 html 个元素,例如 <b>
或 <i>
。我试过做这样的事情(例如):
#!/usr/bin/env perl
#
# Create a simple XML document
#
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0', 'utf-8');
my $root = $doc->createElement("resources");
my $tag = $doc->createElement("string");
$tag->setAttribute('name'=>'no_messages');
$tag->appendText("You have <b>no</b> messages");
$root->appendChild($tag);
$doc->setDocumentElement($root);
print $doc->toString();
但我最终得到的是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="no_messages">You have <b>no</b> messages</string>
</resources>
我正在寻找的是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="no_messages">You have <b>no</b> messages</string>
</resources>
由于不支持innerHTML
,您必须手动添加文本和标签:
my $tag = $doc->createElement("string");
$tag->setAttribute('name'=>'no_messages');
$tag->appendText("You have ");
$b = $doc->createElement("b");
$b->appendText("no");
$tag->appendChild("b");
$tag->appendText(" messages");
那个,还是用parser.
XML::LibXML::Element
对象有一个 appendWellBalancedChunk
方法可以完全满足您的要求。
这是一个基于您自己的示例代码的演示
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new(qw/ 1.0 utf-8 /);
my $root = $doc->createElement('resources');
my $tag = $doc->createElement('string');
$tag->setAttribute(name => 'no_messages');
$tag->appendWellBalancedChunk('You have <b>no</b> messages');
$root->appendChild($tag);
$doc->setDocumentElement($root);
print $doc->toString;
输出
<?xml version="1.0" encoding="utf-8"?>
<resources><string name="no_messages">You have <b>no</b> messages</string></resources>