469,312 Members | 2,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Parsing <content:encoded> through JS

Hello,

I am here after head-banging with this issue for several days. I am
trying to parse a feed using JS and have stucked on the
<content:encoded> tag. I have tried getElementsbyTagName but its not
working.

Any suggstions?

Kashif

Apr 8 '06 #1
7 6266


kashaziz wrote:
I am here after head-banging with this issue for several days. I am
trying to parse a feed using JS and have stucked on the
<content:encoded> tag. I have tried getElementsbyTagName but its not
working.


Is that well-formed XML with namespaces meaning content is a prefix and
declared e.g. with
xmlns:content="http://example.org/2006/whatever"
?
Then with Mozilla and with Opera use e.g.

xmlDocument.getElementsByTagNameNS('http://example.org/2006/whatever',
'encoded')
instead of getElementsByTagName.
For IE 6 and MSXML 3 you can use XPath e.g.
xmlDocument.setProperty('SelectionLanguage', 'XPath');
xmlDocument.setProperty('SelectionNamespaces',
'xmlns:content="http://example.org/2006/whatever"');
var encodedElements = xmlDocument.selectNodes('//content:encoded');

Both getElementsByTagNameNS and selectNodes can also be called on
element nodes instead on on the document node. But note that selectNodes
takes an XPath expression, use a relative expression e.g.
element.selectNodes('content:encoded')
or
element.selectNodes('.//content:encoded')
to find elements relative to the element the method is called on.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 8 '06 #2
Martin Honnen wrote:
kashaziz wrote:
I am here after head-banging with this issue for several days. I am
trying to parse a feed using JS and have stucked on the
<content:encoded> tag. I have tried getElementsbyTagName but its not
working.


Is that well-formed XML with namespaces meaning content is a prefix and
declared e.g. with
xmlns:content="http://example.org/2006/whatever"
?
Then with Mozilla and with Opera use e.g.

xmlDocument.getElementsByTagNameNS('http://example.org/2006/whatever',
'encoded')
instead of getElementsByTagName.
For IE 6 and MSXML 3 you can use XPath e.g.
[...]


The Gecko DOM has XPath support, too.

<URL:http://xulplanet.com/references/objref/>
<URL:http://kb.mozillazine.org/XPath>
<URL:http://developer.mozilla.org/en/docs/XPath>
PointedEars
Apr 8 '06 #3


Thomas 'PointedEars' Lahn wrote:

The Gecko DOM has XPath support, too.


Right, Opera 9 will also implement the W3C DOM Level 3 XPath API (only a
W3C note, not a recommendation) but to access elements in namespaced
documents safely getElementsByTagNameNS is sufficient and much easier to
use than the W3C DOM Level 3 XPath API.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 8 '06 #4
The Feed is:
<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/1.5.2" -->
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"


<channel>
<title>Apnijobs.com: Jobs in Pakistan</title>
<link>http://www.apnijobs.com</link>
<description>Work for pleasure, not for creating more work - Search
Jobs in Pakistan and around the World.</description>
<pubDate>Mon, 27 Mar 2006 10:35:13 +0000</pubDate>
<generator>http://wordpress.org/?v=1.5.2</generator>
<language>en</language>

<item>
<title>Teachers Required for Nursery / Kindergarten and Junior School
(Karachi)</title>
<link>http://www.apnijobs.com/teachers-required-for-nursery-kindergarten-and-junior-school-karachi.html</link>
<comments>http://www.apnijobs.com/teachers-required-for-nursery-kindergarten-and-junior-school-karachi.html#comments</comments>
<pubDate>Mon, 27 Mar 2006 10:34:17 +0000</pubDate>
<dc:creator>Ali Asgher</dc:creator>

<category>Teaching</category>
<category>Listing</category>
<category>Jobs</category>
<guid>http://www.apnijobs.com/teachers-required-for-nursery-kindergarten-and-junior-school-karachi.html</guid>
<description><![CDATA[<p>School of excellence requires teachers for
Nursery / Kindergarten and Junior school Science teach...</p>
]]></description>
<content:encoded><![CDATA[<p>School of excellence requires teachers
for Nursery / Kindergarten and Junior school Science teacher for
immediate appointment. </p>

<p>Contact 021-4981303, 4816345.</p>

<p>Address: D-2, Block-4, Gulshan-e-Iqbal, Karachi.</p>

<p>E-mail: so*@cyber.net.pk</p>
]]></content:encoded>
<wfw:commentRSS>http://www.apnijobs.com/teachers-required-for-nursery-kindergarten-and-junior-school-karachi.html/feed/</wfw:commentRSS>
</item>
--------------------------------------------------------------------------------------------------------

and the code is:
for (var i=0; i<this.feeditems.length; i++){
if(i==this.feed_id){
var feedfound=1
this.title[i]=this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue

this.link[i]=this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue

this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue

this.pubdate[i]=this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue
tickercontent = "<a
href="+this.link[i]+">"+this.title[i]+"</a>"+"<br/>"

}
}

Apr 8 '06 #5


kashaziz wrote:
The Feed is:
<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/1.5.2" -->
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/" <content:encoded><![CDATA[<p>School of excellence requires teachers
for Nursery / Kindergarten and Junior school Science teacher for
immediate appointment. </p>

<p>Contact 021-4981303, 4816345.</p>

<p>Address: D-2, Block-4, Gulshan-e-Iqbal, Karachi.</p>

<p>E-mail: so*@cyber.net.pk</p>
]]></content:encoded>


Then use the already suggested code, the above is XML with namespaces so
with Mozilla and Opera make use of the W3C DOM Level 2 namespace aware
methods like getElementsByTagNameNS e.g.
var encodedElements = xmlDocument.getElementsByTagNameNS(
'http://purl.org/rss/1.0/modules/content/',
'encoded'
);
Then you get a DOM NodeList the same as with getElementsByTagName so you
should know how to loop through. Only note that the contents of those
content:encoded elements seems to be a single CDATA section node to
escape HTML markup snippets. You can read out that HTML markup snippets
as text (e.g. encodedElements[0].firstChild.data) but inside of a CDATA
section it will not be parsed into nodes.

And for IE 6/MSXML 3 you can use selectNodes and XPath 1.0 to get a DOM
NodeList as suggested
xmlDocument.setProperty('SelectionLanguage', 'XPath');
xmlDocument.setProperty('SelectionNamespaces',
'xmlns:content="http://purl.org/rss/1.0/modules/content/"');
var encodedElements = xmlDocument.selectNodes('.//content:encoded');
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 9 '06 #6
I have tried var encodedElements = xmldata.getElementsByTagNameNS(
"http://purl.org/rss/1.0/modules/content/", "encoded"); but it returns
a 0 length value.

Apr 11 '06 #7
Any suggestion guys?

Apr 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Francesco Moi | last post: by
17 posts views Thread by ccdrbrg | last post: by
4 posts views Thread by David Lozzi | last post: by
12 posts views Thread by Christian Roth | last post: by
5 posts views Thread by magix | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.