Jump to content

check xml tags


zaheer
 Share

Recommended Posts

hi, i'm trying to check an xml/rss url, for specific tags using xml and javascript.I tried using the SelectSingleNode method, but this works only if the xml document is consistent. For example if one item had a date tag, and the second item didn't, this would cause my page to crash.I want the structure to be so that:XmlNode node = xmlDocument.SelectSingleNode("some tag to look for");if (node != null)but the above doesn't work. please help.

Link to comment
Share on other sites

You can use getElementsByTagName("TheNameOfTheTagYouAreLookingFor"), and that is actually what will work in all browsers (actually, scratch that... you're not in a browser environment, are you?). If you want to limit the selection in a certain area rather than the whole document, you just use the method on that node. eg.

XmlNode node = xmlDocument.getElementsByTagName("some tag to look for");if (node.length != 0)

Link to comment
Share on other sites

When i tried using the XmlNode node = xmlDoc.getElementsByTagName("name of tag");if (node.length != 0)the IE browser keeps saying "expected ;" and refers to the line of this code. I tried changing the xmlNode node part to var f, and then saying:(f.length != 0 )but this had problems if tags vary from item to item, and some are present in some items, but not in others. Please can u help me.

Link to comment
Share on other sites

Again, if you want to limit the selection in a certain area rather than the whole document, you just use the method on that node.If you need to check if an element (item) exists inside another element (a container), you need to first go to the container, and search the item from there. Like:

var node = xmlDoc.getElementsByTagName("td")[0].getElementsByTagName("span");if (node.length != 0)

(this assumes a "td" element is known to exist somewhere in the document, and that you're searching if there's a "span" in the very first "td" element)

Link to comment
Share on other sites

here's the xml file, i'm having problem with. as u can see there are 2 items, some with the same tags some with different tags. <channel><title>edos</title><link>http://www.edos.com</link><description>Sample RSS feed><language>en-gb</language><lastBuildDate>Thu, 12 Feb 2008</lastBuildDate><copyright>edos</copyright><ttl>15</ttl><item><title>edos1</title><description>Briefing session</description><category>edos c</category><ele490:location>edos room 101</ele490:location><ele490:date>Thu, 24 Jan 2008</ele490:date><ele490:duration>01:00:00</ele490:duration><link>https://www.edos.com</link><pubDate>Thu, 17 Jan 2008 10:22:01 GMT</pubDate></item><item><title>ede102</title><description>Briefing session</description><category>ELE490</category><ele490:location>edos lab</ele490:location><ele490:date>Thu, 14 Feb 2008</ele490:date><ele490:starttime>10:00:00</ele490:starttime><link>https://www.edos1.com</link><pubDate>Thu, 17 Jan 2008 10:22:01 GMT</pubDate></item></channel></rss>i want to check if each tag individually exists and print them on screen using the following:document.write("<td>");document.write("<font face= 'consolas' size='3'>");document.write("Startime:");document.write(x.getElementsByTagName("ele490:starttime")[0].childNodes[0].nodeValue);document.write("</td>");document.write("</tr>");document.write("</font>");the problem i'm having is if for example above, the "ele490:starttime tag, exists in the second item, but not in the first item.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...