Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of an XML element

Tags:

python

perl

In the XML example below, how can I can get the value of the <data> element? In this case, I would like XML parser to return the value of node as a string which should return

This is my first web page<br/><p>test123</p><p>How exciting</p>

(i.e. an XML fragment).

I have been trying to use Perl (XML::SimpleObject) or Python (miniDOM). Both of them can only return the text value of the <data> element which is "This is my first web page"

<dataset>
<data>
This is my first web page<br/>
<p>test123</p>
<p>How exciting</p>
</data>
</dataset>
like image 544
bassman Avatar asked Dec 18 '25 14:12

bassman


1 Answers

The XML::Simple module is altogether too simple and leaves a lot of work to be done by the programmer. I haven't tried XML::SimpleObject but I would encourage you to use either XML::Twig or XML::LibXML, which are both tried and tested and will cope with all the complexity of the full XML specification.

This sollution uses XML::Twig, and simply parses the data, looks up the <data> element and prints its contents.

use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig->new;
$twig->parse(\*DATA);

my ($data) = $twig->get_xpath('/dataset/data');
print $data->xml_string;

__DATA__
<dataset>
<data>
This is my first web page<br/>
<p>test123</p>
<p>How exciting</p>
</data>
</dataset>

output

This is my first web page<br/><p>test123</p><p>How exciting</p>
like image 148
Borodin Avatar answered Dec 20 '25 16:12

Borodin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!