Jump to content

Php Aplication Help.


migroo

Recommended Posts

I can't understand why it is doing this to me! Okay I have a xml document named "name.xml":

<?xml version="1.0" ?> <person>	 <name>Laura Long</name>	 <age>34</age> </person>

I have a page that is going to load and display the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><html><head><script type="text/javascript">var xmlDoc;if (window.XMLHttpRequest)  {  xhttp=new XMLHttpRequest();  }else // Internet Explorer 5/6  {  xhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xhttp.open("GET","name.xml",false);xhttp.send("");xmlDoc=xhttp.responseXML;var x=xmlDoc.getElementsByTagName("person");function show(i){name=(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);age=(x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue);txt="Name: "+name+"<br />Title: "+age+"<br />;document.getElementById("show").innerHTML=txt;}</script></head><body><div id='show'>Names and age will be here</div><br /><script type="text/javascript">document.write("<table border='1'>");for (var i=0;i<x.length;i++)  {   document.write("<tr onclick='show(" + i + ")'>");  document.write("<td>");  document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);  document.write("</td><td>");  document.write(x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue);  document.write("</td></tr>");  }document.write("</table>");</script></body></html>

It just displays:

Names and age will be here-
and nothing else!I believe my problem is somewhere in the body part of the document but I am not sure I have bean messing around with if for 3 hours and am getting very angry with it.So any help would be MUCH appreciated. THANK YOU.
Link to comment
Share on other sites

Edit: I've always seen HTML written with that. I guess not then. Last time I tried to create a <option> list with JavaScript and it was not displaying. But I'm pretty sure w3schools is spot on.

Link to comment
Share on other sites

Okay I will give it a try. This is not my code I have just adjusted some code w3schools gives. Here is a like the where is am grabbing it.The lesson:http://w3schools.com/xml/xml_applications.aspThe "try it yourself":http://w3schools.com/xml/tryit.asp?filename=tryxml_appThanks again for the input!

Link to comment
Share on other sites

There example works just fine but when I try to use it with another xml file it just doesn't wan't to work and I have no idea why I can't see what I have done to it that would change it all that much.I just changed the file that its supposed to open and changed the directories with so that it will read the new file. I don't know if I just missed something or what :). Thank you for reading a posting.

Link to comment
Share on other sites

I have no clue where the error is. So I am looking for any error I can find. I am just writing this in notepad++ so I don't have any sort of checker.

Link to comment
Share on other sites

Well, that's the problem. Every browser has some sort of error console that will display Javascript error messages. You can also download something like Firebug for Firefox and it will show error messages there as well. A major part of programming is responding to error messages, so you need to figure out where the error messages are going in your browser and make sure you look for them. They should tell you which line of code it detected an error on and what the error message was.

Link to comment
Share on other sites

Since you posted this in the PHP forum maybe the PHP way would be useful to you. This example uses the SimpleXMLElement class built into PHP.

$xml = new SimpleXMLElement("person.xml", 0, true);

The SimpleXMLElement __construct method accepts five parameters, and I have used three in this example (really you only need 1). The first is data which is the xml data. You can either enter the xml data in here or a path to the file. If you use a path, you need the second two parameters. The second parameter is any options you wish to pass so I put a 0 here for "no options". Then the third parameter accepts true or false as to whether or not the data parameter is a url to file. So, I put true here to say that "person.xml" is a path to a file.Whew! Ok now $xml is holding an object that holds the same data as your xml file. So to access the name node, use the following code (assuming you know how objects work). Keep in mind that the top tier node (<person>) becomes the top level of the object.

echo $xml->name;

Hope this helps if you choose not to use javascript at any point.*editIn fact now that I think about it, if I want to load xml using a javascript event such as onclick, I make an ajax request for the php file that returns the xml I need. Not sure if this is always the best way, but it works well for me in all cases so far.

Link to comment
Share on other sites

Thank you Rufus! I like PHP much better than javascript. I never knew that there was a way to do this with PHP. So thank you again!PS. sorry I posted this here I forgot I was under PHP.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...