如何使用 Perl 的 XML::Twig 将属性添加到子元素?
我有一个像这样的 XML 字符串:
<DATA>
<CHILD_DATA ATVAL="value1"/>
<CHILD_DATA />
</DATA>
我想要的最终输出是:
<DATA>
<CHILD_DATA ATVAL="value1"/>
<CHILD_DATA ATVAL="value2"/>
</DATA>
My twig $t
is at 。现在我想向第二个
添加一个属性。该属性为 ATVAL="value2"
。我尝试了以下方法:
$t->last_child('CHILD_DATA')->set_att{"ATVAL","value2"};
这不起作用。这段代码有什么问题?还有其他方法可以做到这一点吗?
I have an XML string like this:
<DATA>
<CHILD_DATA ATVAL="value1"/>
<CHILD_DATA />
</DATA>
The final output I want is:
<DATA>
<CHILD_DATA ATVAL="value1"/>
<CHILD_DATA ATVAL="value2"/>
</DATA>
My twig $t
is at <DATA>
. Now I want to add an attribute to the second <CHILD_DATA />
. The attribute is ATVAL="value2"
. I tried the following:
$t->last_child('CHILD_DATA')->set_att{"ATVAL","value2"};
This didn't work. What's wrong with this code? Is there another way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如乔恩向您暗示的那样,您发布的代码中有语法错误。您应该看到如下编译错误:
但是,您可能已在答案中输入了代码,因此该代码与您实际执行的操作不匹配。始终将实际代码放入您的问题中,而不是重新键入它,并且尽可能发布完整的程序。当您发布程序时,我不必从头开始调试我认为您可能正在做的事情。 :)
这是一个可以完成您想要的操作的程序:
As Jon hinted to you, you have a syntax error in the code you posted. You should have seen a compile error like:
However, you might have typed the code into your answer so that code doesn't match what you are actually doing. Always put the actual code into your question rather than re-typing it, and always post a full program when you can. When you post your program, I don't have to start from scratch to debug what I think you might be doing. :)
Here's a program that does what you want:
只是一些想法:
多次发布相同的问题
时代不会讨好任何人
来帮助您。
你的代码在语法上根本不符合
正确,所以我并不感到惊讶你
遇到问题。
为什么不包括你的错误
得到?也许这可能会减少一些
阐明问题?
Just a few thoughts:
Posting the same question multiple
times is not going to endear anyone
to helping you.
Your code isn't even syntactically
correct, so I'm not surprised you're
experiencing problems.
Why not include the errors you are
getting? Perhaps that might shed some
light on the problem?