admin管理员组

文章数量:1297010

I have a log entry as below:

<Event Timestamp="2017-06-14T10:17:09.313991+10:00" Level="INFO" Identity=""><Message>Accessed Page: </Message></Event>

I'd like to extract the attribute like Timestamp, Level with Cheerio. What I did is like this:

const $ = cheerio.load(line)
console.log($("Event").attr('Timestamp'))

However, I just get undefined in the console. Could you please advise where is the problem?

I have a log entry as below:

<Event Timestamp="2017-06-14T10:17:09.313991+10:00" Level="INFO" Identity=""><Message>Accessed Page: </Message></Event>

I'd like to extract the attribute like Timestamp, Level with Cheerio. What I did is like this:

const $ = cheerio.load(line)
console.log($("Event").attr('Timestamp'))

However, I just get undefined in the console. Could you please advise where is the problem?

Share Improve this question edited Nov 26, 2022 at 1:25 ggorlen 57.4k8 gold badges110 silver badges154 bronze badges asked Jun 22, 2017 at 1:53 SuwenSuwen 1211 gold badge1 silver badge6 bronze badges 3
  • What do you get if you log $("Event")? – tavnab Commented Jun 22, 2017 at 1:58
  • 3 Hi tavnab, thanks for your asking. It displays the JSON object information of the Event. From the JSON object I found the attribute name is low-cased. So I change the code to console.log($("Event").attr('timestamp')). It works. thanks for your asking again. – Suwen Commented Jun 22, 2017 at 2:09
  • 1 Cool, good to know :) feel free to answer your own question & accept it, if that was the issue – tavnab Commented Jun 22, 2017 at 2:11
Add a ment  | 

1 Answer 1

Reset to default 5

.attr() is the correct Cheerio method call, but this XML is being parsed in the default HTML mode. In HTML mode, Cheerio lowercases attributes, which you can see with console.log($.html()); after loading it.

Adding {xml: true} preserves casing, assuming you're dealing with XML:

const cheerio = require("cheerio"); // 1.0.0-rc.12

const xml = `
<Event Timestamp="2017-06-14T10:17:09.313991+10:00" Level="INFO" Identity=""><Message>Accessed Page: </Message></Event>
`;

const $ = cheerio.load(xml, {xml: true});
console.log($("Event").attr("Timestamp")); // => 2017-06-14T10:17:09.313991+10:00

If this is actually HTML, then you can extract the field using a lowercased attribute name:

const $ = cheerio.load(html);
console.log($("Event").attr("timestamp")); // => 2017-06-14T10:17:09.313991+10:00

There's also a lowerCaseTags option that can be useful for situations like these.

本文标签: javascriptHow to extract uppercased attributes with CheerioStack Overflow