danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 190 forks source link

Parsing XML attributes #214

Closed Ziker22 closed 7 years ago

Ziker22 commented 7 years ago

Hi, Is there a way to get custom xml attributes when promise is resolved ? example

<item segment="home">
<title>blah</title>
<link>
blabla.com
</link>
</item>

I want to parse segment="home" part

thx in advance

danmactough commented 7 years ago

Hey, @Ziker22. That's an extremely non-standard thing to see. But the good news you can do it!

As you are parsing, each item will have lots of extra properties. If we assume an item is named item, then you'll see that item['rss:@'] (if it's an rss feed) or item['atom:@'] (if it's an atom feed) will be an object of name/value pairs for any attributes of the item. So in your example:

console.log(item['rss:@']['segment']);
// prints "home"

Hope that helps.

clakech commented 6 years ago

Thanks @danmactough, this answer helps me ;)

clakech commented 6 years ago

@danmactough what if my item's properties have extra attributes like:

<item segment="home">
<title anotherAttribute="hello">blah</title>
<link>
blabla.com
</link>
</item>

Any chance to get the anotherAttribute value 'hello' on item.title ?

danmactough commented 6 years ago

@clakech yup. same thing: item.title['rss:@']['anotherAttribute']