Cheerio: How to parse XML with meta, link etc. tags?

Created on 20 Feb 2012  ·  7Comments  ·  Source: cheeriojs/cheerio

I trying to parse XML with cheerio. XML contains tags. And the following script is return empty string in that case:

$('foo').find('link').text()

How to parse XML with tags , and similar?
Thanks in advance.

❌ Bug

Most helpful comment

You can now do:

var cheerio = require("cheerio"),
    html = '<link>foo</link>',
    $ = cheerio.load(html, { xmlMode: true });

console.log($("link").text())

Which will return:

foo

All 7 comments

Thanks for bringing this up. I think it's being caused by a bug in htmlparser2. I've submitted an issue here: FB55/node-htmlparser#16

You can now do:

var cheerio = require("cheerio"),
    html = '<link>foo</link>',
    $ = cheerio.load(html, { xmlMode: true });

console.log($("link").text())

Which will return:

foo

Thank you. Already use it. :)

But working with tag remains incorrect.

var cheerio = require("cheerio"),
$ = cheerio.load('<root></root>', { xmlMode: false });
$('root').append($('<link></link>').text('foo'));
$('root').append($('<someTag></someTag>').text('some text'));
console.log($.html());

The code above returns the following:

<root><link/>foo<someTag>some text</someTag></root>
instead
<root><link>foo</link><someTag>some text</someTag></root>

@SlavaB xmlMode needs to be true.

It doesn't make any difference, true or false.. The result is the same.

Hello, i need read a XML like this:

<Main>
<Child ID="1" Type="ABC" Dealer="" Name="Vitor" Street="ABC st" Number="2350" N="Local" City="São Paulo" State="SP" PostalCode="01111-100" Parking="Não" Lat="-46.6624320" Long="-23.5578566"> 
<People="90" Saturday="09h" Holiday="Close"/>
</Child>
</Main>

But I can't. Somebody Help me? I want structured this XML code to a Data Base:
Name: 'Vitor'
Street: 'ABC st'
Number: '2350'

Was this page helpful?
0 / 5 - 0 ratings