Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-21-2012, 06:52 PM   PM User | #1
marilynn.fowler
Regular Coder

 
Join Date: Aug 2002
Location: San Francisco
Posts: 442
Thanks: 19
Thanked 15 Times in 15 Posts
marilynn.fowler is an unknown quantity at this point
Calling a subset of an XML doc

I'm using an XMLHttpRequest to call an XML document.

Code:
xmlhttp.open("GET","gallery.xml",false);
xmlhttp.send();
var xmlDoc = xmlhttp.responseXML; 
var x = xmlDoc.getElementsByTagName("photo");
How do I retrieve a subset of the photo elements - for example, retrieve only those with "insect" in the category node?
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx
marilynn.fowler is offline   Reply With Quote
Old 05-21-2012, 10:30 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Show some sample XML. May be trivial, may be harder. Depends on the XML contents.

If possible, show at least two of the nodes, one with and one without the data you are after.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 05-22-2012, 06:48 AM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
as a rough estimate, querySelectorAll() should work.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-22-2012, 07:47 AM   PM User | #4
marilynn.fowler
Regular Coder

 
Join Date: Aug 2002
Location: San Francisco
Posts: 442
Thanks: 19
Thanked 15 Times in 15 Posts
marilynn.fowler is an unknown quantity at this point
Code:
<photos>
     <photo big="images/lilypads.jpg" thumb="images/lilypads_t.jpg">
          <name>Lily Pads</name>
          <description>Saigon: I'd lie to spend more time there.</description>
          <category>travel, flora</category>
     </photo>

     <photo big="images/buddha.jpg" thumb="images/buddha_t.jpg">
          <name>Sleeping Buddha</name>
          <description>Nha Tran: I liked it because it had big feet</description>
          <category>travel, buddha</category>
     </photo>
</photos>
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx

Last edited by marilynn.fowler; 05-23-2012 at 02:28 AM.. Reason: typos
marilynn.fowler is offline   Reply With Quote
Old 05-22-2012, 01:44 PM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
that is going to be problematic. first I’d say you have un-normalised data in the <category> element, so you would actually need a script to find your matches. I would recommend to use a <category> tag for each category so you can match more easily. but without the use of XPath I see no simple way to achieve that without an explicit text search.

and that XML is not well-formed ...
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-22-2012, 05:39 PM   PM User | #6
marilynn.fowler
Regular Coder

 
Join Date: Aug 2002
Location: San Francisco
Posts: 442
Thanks: 19
Thanked 15 Times in 15 Posts
marilynn.fowler is an unknown quantity at this point
That's what I get for typing a on my iPad instead of cutting and pasting. Can JavaScript understand xpath expressions?
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx
marilynn.fowler is offline   Reply With Quote
Old 05-22-2012, 05:55 PM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
JavaScript - yes, browsers - depends on the browser
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-22-2012, 06:58 PM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
if I understand correctly what you are trying to do, maybe the below will help - of course you will have to reconstruct whatever you want to be shown from the nodes in the xml, but this example shows the text from the name node if the term in the search box matches part or all of the category.

You have one major problem with your xml - you are self-closing your photo tags:
Code:
<photo big="images/lilypads.jpg" thumb="images/lilypads_t.jpg" />
you shouldn't do that as you have a closing tag after the other sub-nodes.

Code:
<body>
<input type="text" id="inp"/><input type="button" value="search" onclick="searchPics()"/> 
<div id="results"></div>
<script type="text/javascript">
var xml;

function downloadUrl(callback) {
url="gallery.xml"
var xmlFile = window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');
	xmlFile.onreadystatechange = function() {
   if (xmlFile.readyState == 4) {
     callback(xmlFile, xmlFile.status);
   }
 };
xmlFile.open("GET", url, true); 
xmlFile.send(null);
}


downloadUrl(function(data) {
  xml = data.responseXML; 
  });

function searchPics(){
document.getElementById("results").innerHTML=""
var term=document.getElementById("inp").value
var pics = xml.getElementsByTagName("photo");
for (var i = 0; i < pics.length; i++) {
  var cat =pics[i].getElementsByTagName("category")[0]
  if (cat.firstChild.nodeValue.match(term)){
  document.getElementById("results").innerHTML+=pics[i].getElementsByTagName("name")[0].firstChild.nodeValue+"<br>"
		}
	}

}

</script>

</body>
xelawho is offline   Reply With Quote
Old 05-23-2012, 05:34 PM   PM User | #9
marilynn.fowler
Regular Coder

 
Join Date: Aug 2002
Location: San Francisco
Posts: 442
Thanks: 19
Thanked 15 Times in 15 Posts
marilynn.fowler is an unknown quantity at this point
Thank you, xelawho. I had tried code very similar to that and while it gave me a subset of the thumbnails, if I used previous/next buttons, it still cycled through all of the photo elements. I guess my real question is whether it is possible to modify the initial value of x variable so that it only gets the array of photo elements where the category contains insects?

var x = xmlDoc.getElementsByTagName("photo");

Can I initially set x to be xmlDoc.getElementsByTagName("photo[category='insect']") or something along those lines?
And my actual XML document is well formed, I am just pitiful at retyping on an iPad
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx
marilynn.fowler is offline   Reply With Quote
Old 05-23-2012, 07:10 PM   PM User | #10
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by marilynn.fowler View Post
Can I initially set x to be xmlDoc.getElementsByTagName("photo[category='insect']") or something along those lines?
that would be XPath. but getElementsByTagName() only accepts tag names, not more, not less.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-24-2012, 08:10 PM   PM User | #11
marilynn.fowler
Regular Coder

 
Join Date: Aug 2002
Location: San Francisco
Posts: 442
Thanks: 19
Thanked 15 Times in 15 Posts
marilynn.fowler is an unknown quantity at this point
So I'm back to square one. Can anyone point me to a tutorial that deals with this? I took an XML class, but we never touched on this scenario.
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx
marilynn.fowler is offline   Reply With Quote
Old 05-24-2012, 09:51 PM   PM User | #12
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by marilynn.fowler View Post
I guess my real question is whether it is possible to modify the initial value of x variable so that it only gets the array of photo elements where the category contains insects?
why not just push everything that matches your criteria onto a separate array, or am I missing something fundamental here?

Code:
<body>
<input type="text" id="inp"/><input type="button" value="search" onclick="searchPics()"/> 
<div id="results"></div>
<input type="button" value="<" onclick="showPics('down')"/><input type="button" value=">" onclick="showPics('up')"/>
<script type="text/javascript">
var xml;
var tmp;

function downloadUrl(callback) {
url="gallery.xml"
var xmlFile = window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');
	xmlFile.onreadystatechange = function() {
   if (xmlFile.readyState == 4) {
     callback(xmlFile, xmlFile.status);
   }
 };
xmlFile.open("GET", url, true); 
xmlFile.send(null);
}


downloadUrl(function(data) {
  xml = data.responseXML; 
  });

function searchPics(){
tmp=[];
document.getElementById("results").innerHTML=""
var term=document.getElementById("inp").value
var pics = xml.getElementsByTagName("photo");
for (var i = 0; i < pics.length; i++) {
  var cat =pics[i].getElementsByTagName("category")[0]
  if (cat.firstChild.nodeValue.match(term)){
tmp.push(pics[i].getElementsByTagName("name")[0].firstChild.nodeValue)
		}
	}
document.getElementById("results").innerHTML=tmp[0]
}

function showPics(dir){
if (dir=="up"){
tmp.push(tmp.shift())
} else{
tmp.unshift(tmp.pop())
	}
	document.getElementById("results").innerHTML=tmp[0]
}
</script>

</body>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
marilynn.fowler (06-08-2012)
Old 06-08-2012, 02:19 AM   PM User | #13
marilynn.fowler
Regular Coder

 
Join Date: Aug 2002
Location: San Francisco
Posts: 442
Thanks: 19
Thanked 15 Times in 15 Posts
marilynn.fowler is an unknown quantity at this point
Thank you for replying, xelawho. I'm going to go through it line by line to see if I understand how it works. I got sidetracked by other projects, so I know I'm going to have questions, but I appreciate the time you took giving me an answer.
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx
marilynn.fowler is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:43 AM.


Advertisement
Log in to turn off these ads.