Skip to content

Parsing XML with Mojo::DOM: how to get nested nodes? #2166

Answered by Akron
fluca1978 asked this question in Q&A
Discussion options

You must be logged in to vote

There are two ways to deal with namespaces in Mojo::DOM::CSS. Either you define the namespace and use it with | as described here - or you ignore the namespace and escape the colon (that way the namespace prefix is treated as being part of the tag).

use Mojo::Base -strict;
use Mojo::DOM;

my $file = join("\n",<DATA>);

my $dom = Mojo::DOM->new->xml(1)->parse($file);

$dom->find( q{yadda|doc > item}, yadda => 'xocs-ns' )->each(sub { say $_->text });

$dom->find( q{xocs\:doc > item} )->each(sub { say $_->text });

__DATA__

<xocs:doc xmlns:xocs="xocs-ns">
  <item>hello</item>
  <item>world</item>
</xocs:doc>

Replies: 5 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@kraih
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by fluca1978
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants