LIBXML - 如何获取标签的名称?
我有以下内容:
my $string='<entry><name>Bob</name><zip>90210</zip></entry>';
my $parser=XML::LibXML->new();
use HTML::Entities;
my $encodedXml=encode_entities($string,'&\'');
my $doc=$parser->parse_string($encodedXml);
foreach my $text($doc->findnodes("//text()")){
print $text->to_literal,"\n";
}
这会打印出“Bob”和“90210”;
如何获取实际的节点名称...我需要一种方法来获取 xml 树中的所有节点...即“名称”和“zip”
I have the following:
my $string='<entry><name>Bob</name><zip>90210</zip></entry>';
my $parser=XML::LibXML->new();
use HTML::Entities;
my $encodedXml=encode_entities($string,'&\'');
my $doc=$parser->parse_string($encodedXml);
foreach my $text($doc->findnodes("//text()")){
print $text->to_literal,"\n";
}
This prints out 'Bob' and '90210';
How do I get the actual node names...I need a way to get all the nodes within my xml tree....ie 'name' and 'zip'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文本节点没有名称。也许您想要父母的名字?
我认为这会起作用:
我会使用
注意:此较新版本组合了该元素的所有文本子元素,因此如果一个节点具有多个文本子元素,则它是不等效的。不过,它们对你来说应该是等效的。
Text nodes don't have names. Perhaps you want the name of the parent?
I think this will work:
I would use
Note: This later version combines all the text children of the element, so it's not equivalent if a node has more than one text child. They should be equivalent for you, though.
您的代码所做的就是选择
text
节点,这些节点作为您要查找的节点的子节点存在。文本节点是一个单独的实体,并且没有名称。您需要导航到文本节点的父节点,并且该节点将包含标签名称。对于同时包含文本和元素节点的混合内容节点,事情会变得更加棘手,例如
在这种情况下,结构是
What your code does is select the
text
nodes, which exist as children of the nodes you are looking for. A text node is a separate entity, and it does not have a name. You need to navigate to the text node's parent and that node will contain the tag name.Things get trickier with mixed-content nodes that contain both text and element nodes, such as
In this case the structure is