Jump to content

Searching a DOM tree.


nick1

Recommended Posts

Greetings,

 

I'm in the process of learning how JavaScript can be used to work with the DOM.

I have a specific problem I'm trying to solve but unfortunately my skills aren't

advanced enough at this point to know how to quickly solve the problem at hand.

 

Here is an excerpt of the XML file I am working with:

 

<item>
<title>Recipe of the Week: 5-Minute Ginger Pineapple</title>
<link>http://whfoods.org/genpage.php?tname=recipe&dbid=236</link>
<description>Recipe of the Week: 5-Minute Ginger Pineapple</description>
<pubDate>Mon, 11 Feb 2008 08:14:45 -0600</pubDate>
<guid isPermaLink="false">tag:whfoods.org,2008-02-11:whfoods.tname=recipe&dbid=236</guid>
</item>

 

In this XML file are an infinite number of <item> nodes, each containing different information.

 

First, I want to search the XML file for the string "Recipe of the Week", which will be located in a <title> node.

Second, I then want to grab the <title> node's parent node, which is <item>, and all of its children and their information.

All of this information will then be stored in a variable so that I can then access the information I need.

For example, I could then locate and do what I want with the <title>, <link>, <description>, etc. nodes.

 

Unfortunately, I have found little information on how to search an XML file for a specific string.

I'm familiar with regular expressions, but I have a feeling there are more efficient ways of searching

an XML file in this case.

 

I would appreciate your thoughts and recommendations on the correct way to solve this problem.

Also, I would greatly appreciate recommendations on some quality JavaScript/DOM materials, including book recommendations.

 

Thank you for your time,

 

*Nick*

Link to comment
Share on other sites

I've never used JS with an xml file, but if it works the same as a regular HTML file then this shouldnt be too difficult.

 

First you want to get a collection of the title elements by doing

 

var items = document.getElementsByTagName('title');

 

Loop through that array and with every iteration run a check for your string

 

var collection = []; //empty array
for(var x in items){
    //this is the check
    var title_text = items[x].innerHtml;
    var reg_str = /Recipe of the Week/i;
    if(reg_str.test(title_text)){
        var parent_node = items[x].parentNode;
        collection.push(parent_node);
    }
}

 

now collection will be an array of all of the items who has a title with the string in it. To access the other nodes, you can either do

 

collection[0].childNodes[0];  //for the title, if the title is always the first childnode

 

or

 

collection[0].getElementsByTagName('link')[0]; //this will give you the link for that item

 

I havent tested this out so let me know if it doesnt work for you

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.