Getting full XML string from PERL LibXML Parser

丶灬走出姿态 提交于 2019-12-10 23:08:39

问题


I am having following XML which I required to process

<table>
<col1>check1</col1>
<col2>check2</col2>
<col3>check3</col3>
<content>
    <data>gt1</data>
    <data>check_gt1</data>
</content>
</table>

I want to get "<content><data>gt1</data><data>check_gt1</data></content>" from the parser.

My parsing code is as follows,

my $parser = XML::LibXML->new();
my $respDom = $parser->parse_string($xmldata);
print "content is ".$respDom->getDocumentElement->findnodes("//content");

The above code results in the textContent inside the nodes.How can I get the data I mentioned above ?


回答1:


The XML::LibXML::Node objects have a method toString. That's what you need. I found it with a quick search of the XML::LibXML documentation.

use strict;
use warnings;
use XML::LibXML;

my $xmldata = <<'XML';
<table>
<col1>check1</col1>
<col2>check2</col2>
<col3>check3</col3>
<content>
    <data>gt1</data>
    <data>check_gt1</data>
</content>
</table>
XML

my $parser = XML::LibXML->new();
my $respDom = $parser->parse_string($xmldata);
print "content is "
  . $respDom->getDocumentElement->findnodes("//content")->[0]->toString;

This will print:

content is <content>
    <data>gt1</data>
    <data>check_gt1</data>
</content>

In general, I always search for either to_string, as_string, stringify or simply string if I need something like that and am not sure how that works in a specific module. It's almost always one of those.


Update

To only get the inside XML of the <content> element, you have to grab its child nodes and do toString for each of them. The map whole thing needs to be called in list context, or it will throw an error. Note how I changed the . to a , in the print statement.

print "content is "
  , $respDom->getDocumentElement->findnodes("//content")->[0]->childNodes->map(sub {$_->toString});


来源:https://stackoverflow.com/questions/34744470/getting-full-xml-string-from-perl-libxml-parser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!