Jump to content

How to load a specific XML node


chronister

Recommended Posts

Hello,

 

I am in need of some help. I want to load a particular XML node by it's position in the file....

 

example file....

<?xml version="1.0" encoding="utf-8"?>
<questions>

<question>
  <q>What is FSB?</q>
  <a>The Front Side Bus (FSB) is the bus that carries data between the CPU and the northbridge (memory controller hub MCH or an integrated memory controller IMC in Intel systems ). It serve as a connection between the CPU and the rest of the hardware via a so-called chipset.</a>
</question>

<question>
  <q>What is a UPS?</q>
  <a>An uninterruptible power supply (UPS), also known as a battery back-up, provides emergency power and, depending on the topology, line regulation as well to connected equipment by supplying power from a separate source when utility power is not available.</a>
</question>

<question>
  <q>What are Vcore and Vi/o?</q>
  <a>ANSWER</a>
</question>

</questions>

 

I am looking for a JavaScript equivalent to PHP's simpleXML object type syntax...

$xml->question[1]->a  to get the a node in the second question node.

 

I am using jQuery with this if it helps simplify code at all.

 

Thanks in advance for help.

 

Nate

Link to comment
Share on other sites

Ok... doing some playing around I seem to be making progress...

 

I have drilled down and I believe that I am in the target node with this..

 

var node = xml.childNodes[0].getElementsByTagName('question')[theId].getElementsByTagName('a') 

 

But I cannot figure out how to get the actual text for that particular node... I have tried

 

alert(node.length); and I get a response of 1, so It seems to be finding the node, node.innerHTML gives me undefined, and node.text() throws an error.

 

I am so close... where do I go from here to accomplish this?

 

Thanks,

 

nate

Link to comment
Share on other sites

hahahaha.... I thank you for the help Dj Kat, but the link you have provided is to this thread... the one I have started and posted to myself... lol was this a typo?

 

I have posted twice (3 times now) and I am almost there... but cannot quite figure out what I need to use to get the text of the partcular node...

 

Thanks,

 

Nate

Link to comment
Share on other sites

Ok, just for anyone else who might find this helpful.... here is how I accomplished what I wanted.

 

<script type="text/javascript">
$().ready(function(){

$('.changeAnswer a').click(function(){
	var theId = $(this).attr('id'); // get the id of item clicked

	$.ajax({ // use jquery ajax function
		type: "GET",
		url: "<?php echo $file; ?>", 
		dataType: "xml",
		success: function(xml){
			$(xml).find('question').each(function(){ // loop through the xml file
				var id = $(this).attr('id') // get id of current item

				if(id == theId){ // if the clicked id matches current id
					q = $(this).find('q').text(); // get text of q node
					a = $(this).find('a').text(); // get text of a node

                // call modal function
					$.modal('\ 
						<div id="answerForm">\
						  <h1>Add/Change Answer</h1>\
							<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">\
							<p class="question" >'+q+'</p>\
							   <textarea name="answer" rows="8" id="answer" >'+a+'</textarea>\
							   <input type="hidden" value="'+theId+'" name="updateId" />\
							   <input class="submit" type="submit" name="updateXML" value="Update XML" />\
							</form>\
						</div>\
					'); // close modal
				} // close if                     
			 }); //close each(
		}// close success
	}); // close ajax
return false;
});
});
</script>

 

 

I wanted to access the node directly without having to loop through all of em, but could not target the node and get it's text, so I did it this way..... loop through and run an if statement to compare the id's... if they match, then set vars, and call modal window.

 

This is an accordian type list with a question and answer pulled from an XML file that has a form to add or update an answer and then re-write the xml file with the new information.

 

Hope someone finds this helpful.

 

Nate

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.