Jump to content

Help With Updating Xml Documents.


migroo

Recommended Posts

Can anyone help me I think I am just missing something simple but I can't find out how to use a text field to update my xml document.Thanks.

Link to comment
Share on other sites

Okay I am sorry I really should have said more.I have a page that is going to display a persons date of birth, death, age, and name. This page is going to need to be easily updated about 3 times a week by a fried of mine who knows absolutely nothing about programing. I have created a simple example problem that works on the same principles to tinker with. I have an admin page which has a text field that I want to use to update a xml file and the page every one will see reads of the xml file. Now I have the main page displaying the xml file just fine my problem is that I am having trouble getting the text area to update my xml file. I created a 4th page that I put between the admin page and the xml file it takes the info and is supposed to use it to update the xml file.Here is the code for the admin page....(Saved as Forum post.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head></head><body><form action="/insert.php" method="post"> To: <input type="text" name="adress" /> <br /> <input type="submit" value="Send" /></form></body></html>

Here is the code for the page that receives the info from the text area....(Saved as insert.php)(The loader.js file is my parser file I have it pasted below.)

<html><head><script type="text/javascript" src="loader.js"></script></head><body><script type="text/javascript">xmlDoc=loadXMLDoc("Note.xml");x=xmlDoc.getElementsByTagName("to")[0].childNodes[0];x.replaceData(0,5,"<?php echo $_POST["adress"]; ?>");</script></body></html>

Here is the xml file......(saved as Note.xml)I know there are more nodes here to display and update than I have text fields for I am just going for updating one of them.

<?xml version="1.0" ?><message> <to>Quinn</to> <from>Eric</from> <body>  Hello hows it going. </body></message>

Here is my parser file.... (Saved as loader.js)

function loadXMLDoc(dname) {var xmlDoc;if (window.XMLHttpRequest)  {  xmlDoc=new window.XMLHttpRequest();  xmlDoc.open("GET",dname,false);  xmlDoc.send("");  return xmlDoc.responseXML;  }// IE 5 and IE 6else if (ActiveXObject("Microsoft.XMLDOM"))  {  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  xmlDoc.async=false;  xmlDoc.load(dname);  return xmlDoc;  }alert("Error loading document");return null;}

Well I hope I have explained my problem well enough for you to help me if there is anything I missed please tell me and thanks for helping me.

Link to comment
Share on other sites

No I don't. The second page the one between the admin and the xml file was supposed to rewrite the xml file. But I think the coding is wrong.

Link to comment
Share on other sites

Okay. Do you have any suggestions as to how I could go about making a file that will write new information to my xml file? Thanks for the help by the way.

Link to comment
Share on other sites

JavaScript can update your XML object. You're already using DOM techniques to access data in your XML object. Just use the same techniques to write new data to the object. Then post the raw text of your object back to PHP and write it to your server.

Link to comment
Share on other sites

Alright I will give it a try. Is there any where I could find an example to look at.And thanks :)

Link to comment
Share on other sites

Okay I have a php file that will create an xml file. The only problem now is, it is not putting the xml code in the file.Here is the PHP code.

<?php$file=fopen("Note2.xml","x") or exit("Unable to open file!");echo "<?xml version='1.0'?>";echo "<Note>";echo "<to>Jubaba</to>";echo "<from>Tove</from>";echo "<body>Remember me this weekend</body>";echo "</Note>";?>

It creates a empty xml file labeled as Note2.xml. What do I need to do to get it to include the content.It just does exactly what I tell it to do, it writes "jubabatoveremember me this weekend" What am I doing wrong :)

Link to comment
Share on other sites

Okay I have a php file that will create an xml file. The only problem now is, it is not putting the xml code in the file.Here is the PHP code.
<?php$file=fopen("Note2.xml","x") or exit("Unable to open file!");echo "<?xml version='1.0'?>";echo "<Note>";echo "<to>Jubaba</to>";echo "<from>Tove</from>";echo "<body>Remember me this weekend</body>";echo "</Note>";?>

It creates a empty xml file labeled as Note2.xml. What do I need to do to get it to include the content.It just does exactly what I tell it to do, it writes "jubabatoveremember me this weekend" What am I doing wrong :)

<?php$file=fopen("Note2.xml","x") or exit("Unable to open file!");fwrite($file, "<?xml version='1.0'?>");fwrite($file, "<Note>");fwrite($file, "<to>Jubaba</to>");fwrite($file, "<from>Tove</from>");fwrite($file, "<body>Remember me this weekend</body>");fwrite($file, "</Note>");fclose($file);?>

You have no code writing to the file you have just opened it. Don't forget to close after :)

Link to comment
Share on other sites

You don't use echo to write to a file. You use fwrite() or file_put_contents() -- the latter is available in PHP versions 5+ and saves a bit of trouble.Check the manual: http://us2.php.net/manual/en/function.file-put-contents.phpThe normal thing is to concatenate all your data into a single string and then write it all at once, in a single statement.I have to say, this is a pretty clunky way of generating XML, especially since you're also learning to use it on the client side. Does your employer insist? Is it because you're interested in learning the technology? My own preference would be to use a database like MySql and write my data into my client-side document as JavaScript objects and arrays.

Link to comment
Share on other sites

Thank you very much guys. :) I am doing it this way partly because I want to learn xml and partly because I was having trouble doing it with data bases, my first attempt, and some one told me that I could use xml instead. Thanks again.

Link to comment
Share on other sites

Thanks, I have looked in to DOM some I am about 3/4s of the way through the tutorial right now. I have another question though is there a way to hide part of your code from users because you can press view source and see all the coding. I need to make a password and I don't want anyone to be able to just hit view source code and find the password.

Link to comment
Share on other sites

Thanks, I have looked in to DOM some I am about 3/4s of the way through the tutorial right now. I have another question though is there a way to hide part of your code from users because you can press view source and see all the coding. I need to make a password and I don't want anyone to be able to just hit view source code and find the password.
PHP code is not displayed to the users. Never make a password system with Javascript.
Link to comment
Share on other sites

Thanks! I am glad you said never to make a password system with javascript because I was just getting started making one with javascript well I will give PHP a try. Thanks.

Link to comment
Share on other sites

Where can I find a reference to URIs? I can't find one anywhere. Some one explained them to me once before on this forum but that was about 4 months ago so now the post is gone and I am wishing I had copied it and saved it some where.

Link to comment
Share on other sites

Hmmm.... maybe I am confused. If I am making a link from one page on my site to an other page I would use a directory that looks like this:

<a href="/Other_page.html"> My link </ a>

But if the other page was stored in another file say under the file where my first page was stored it would look something like this I think:

<a href="/My_file/Other_page.html"> My link </ a>

I am not sure what those are called exactly but I thought they where called URIs what I am trying to find out is that the directory would look like if I was trying to create a link back from the other page to the original page. Thanks for the help and sorry I am not expressing my self very well.

Link to comment
Share on other sites

You mean relative paths, I think. They are not URLs exactly, but browsers and servers are designed to resolve partial and relative URLs into complete URLs.The examples you gave are maybe not the best to illustrate it, since having a / character at the start of a path is shorthand for starting at the root directory. So if your site is www.mysite.com, then writing "/" is the same thing as writing your domain name. So it's basically an absolute path. You start at the top and work your way down.If one.html is in the same directory as two.html, then you don't need any slashes at all. If my_dir/ is in the same directory as one.html, and three.html is inside my_dir, you access it with my_dir/three.html . And it doesn't matter how deeply nested one.html is. As long as those relationships stay the same, those relative paths will work.Bur relative is what they are. You cannot get to three.html that way from a file in ANY OTHER directory.Learn what the dots do, while your at it. As in ../file.html or even just simply "."

Link to comment
Share on other sites

Thanks. Now I think I understand what I was doing, a little more anyway. I think I have learned more from this thread in the last 3 days than I have in about 2 weeks reading the tutorials.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...