Go Back   CodingForums.com > :: Client side development > XML

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 10-26-2012, 03:15 PM   PM User | #1
MikeEller
New to the CF scene

 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
MikeEller is an unknown quantity at this point
Get Specific Node Values

Hello All,

I am doing a little xml learning.
Here is what I want to do....

I want to select a node where a child node has a specific value. Once I have that node, I then want to get other specific child node values to use/display in my html.
So, I am searching the xml file for a specific value in a node. Once found, I want to select the parent of that node and then get at the child node values.

Example:

Code:
<?xml version=1.0"?>
  <locations>
    <loc>
      <id>121</id>
      <user>Joe Smith</user>
      <section>AAA</section>
    </loc>
    <loc>
      <id>122</id>
      <user>Bill Jones</user>
      <section>BBB</section>
    </loc>
  </locations>
So I want to search the xml file above and select the "loc" node whose "id" node value = 122. I then want to get the remaining child nodes (user and section) from that selected "loc" node.

I am using javascript to do this.

It seems it should be easy enough to do....I just have not gotten anything to work yet.
Any assistance would be greatly appreciated.

Regards,
Mike
MikeEller is offline   Reply With Quote
Old 10-26-2012, 04:35 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
First: your xml code, first line reads
Code:
<?xml version=1.0"?>
It should be
Code:
<?xml version="1.0"?>
I saved it as location.xml
This html does what you want:

Code:
<!DOCTYPE html>
<html>
<body>
<script>
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","location.xml",false);
xmlhttp.send('');
xmlDoc=xmlhttp.responseXML;

var x=xmlDoc.getElementsByTagName("loc");
for (i=0;i<x.length;i++)
{
	if(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue == 122)
	{
		document.write(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue+"<br />");
		document.write(x[i].getElementsByTagName("user")[0].childNodes[0].nodeValue+"<br />");
		document.write(x[i].getElementsByTagName("section")[0].childNodes[0].nodeValue);
	}
}
</script>
</body>
</html>
sunfighter is offline   Reply With Quote
Old 10-26-2012, 05:53 PM   PM User | #3
MikeEller
New to the CF scene

 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
MikeEller is an unknown quantity at this point
I thought I could do something like that.
I was hoping that xpath would be able to to just allow me to "grab" the node that contained the value and then use the child nodes within.
MikeEller 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 03:41 AM.


Advertisement
Log in to turn off these ads.