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
1 Answer
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
版权声明:本文标题:javascript - How to extract uppercased attributes with Cheerio - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741633607a2389508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论