如何以编程方式通过 XML::Twig 添加实体声明?
我一生都无法理解实体处理的 XML::Twig 文档。
我有一些用 HTML::Tidy 生成的 XML。调用如下:
my $tidy = HTML::Tidy->new({
'indent' => 1,
'break-before-br' => 1,
'output-xhtml' => 0,
'output-xml' => 1,
'char-encoding' => 'raw',
});
$str = "foo bar";
$xml = $tidy->clean("<xml>$str</xml>");
它产生:
<html>
<head>
<meta content="tidyp for Linux (v1.02), see www.w3.org" name="generator" />
<title></title>
</head>
<body>foo bar</body>
</html>
XML::Twig (可以理解)在
处 barfs。我想做一些转换,通过 XML::Twig 运行它:
my $twig = XML::Twig->new(
twig_handlers => {... handlers ...}
);
$twig->parse($xml);
上的 $twig->parse
行 barfs,但我不能弄清楚如何以编程方式添加
元素。我尝试过类似的事情:
my $entity = XML::Twig::Entity->new("nbsp", " ");
$twig->entity_list->add($entity);
$twig->parse($xml);
...但没有快乐。
请帮忙=)
For the life of me I cannot understand the XML::Twig documentation for entity handling.
I've got some XML I'm generating with HTML::Tidy. The call is as follows:
my $tidy = HTML::Tidy->new({
'indent' => 1,
'break-before-br' => 1,
'output-xhtml' => 0,
'output-xml' => 1,
'char-encoding' => 'raw',
});
$str = "foo bar";
$xml = $tidy->clean("<xml>$str</xml>");
which produces:
<html>
<head>
<meta content="tidyp for Linux (v1.02), see www.w3.org" name="generator" />
<title></title>
</head>
<body>foo bar</body>
</html>
XML::Twig (understandably) barfs at the
. I want to do some transformations, running it through XML::Twig:
my $twig = XML::Twig->new(
twig_handlers => {... handlers ...}
);
$twig->parse($xml);
The $twig->parse
line barfs on the
, but I can't figure out how to add the
element programmatically. I tried things like:
my $entity = XML::Twig::Entity->new("nbsp", " ");
$twig->entity_list->add($entity);
$twig->parse($xml);
... but no joy.
Please help =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,一个肮脏但有效的技巧是添加一个虚假的 DTD 声明。
然后,进行解析的 XML::Parser 将假定该实体是在 DTD 中定义的,并且不会对其进行吐槽。
要摆脱假 DTD 声明,您可以输出树枝的根。如果您需要不同的声明,请创建它并替换当前的声明:
A dirty, but efficient, trick in a case like this would be to add a fake DTD declaration.
Then XML::Parser, which does the parsing, will assume that the entity is defined in the DTD and won't barf on it.
To get rid of the fake DTD declaration, you can output the root of the twig. If you need a different declaration, create it and replace the current one:
也许有更好的方法,但下面的代码对我有用:
There maybe a better way, but the code below worked for me: