Click to See Complete Forum and Search --> : Adding to an XML file?
jms318
12-06-2008, 12:34 AM
Ok I'm learning XML and I'm loving it. I decided to make a little address book i open up in my browser. I not only want to be able to view the addresses but i also want to have the option to add to it by a form in a webpage so i do not have to go into the xml file every time i want to add an address. It will always be stored on my machine so i was wondering if their is a way to use client side scripting such as JavaScript to add to the XML. I'm guessing by use of the DOM maybe? I dont know if this is even possible? below is an example of how the xml structure i was hoping for. I've used XSL and have it displaying the information the way i like, but i just have no idea how to add addresses to the xml file without going in each time manually. Sorry for posting such a newbie question. Any help or point into the right direction would be great.
Thanks,
Joe Swager
<contacts>
<contact>
<name>
<first>Joe</first>
<last>Somebody</last>
</name>
<address>
<street> 555 S. Street </street>
<city> Ann Arbor</city>
<state>MI</state>
<zip>55555</zip>
</address>
<type>business</type>
<email>jms318@this.net</email>
<phone>555-555-5555</phone>
</contact>
</contacts>
rpgfan3233
12-06-2008, 05:41 AM
The clean solution requires you to have access to a server-side language like PHP. You would need to use AJAX to load the XML file, add an entry via the DOM, etc. Alternatively, you could omit AJAX and just use intermediate pages that would fulfill the same purpose. This way if you disabled JavaScript then you would still be able to use the page. Either way, something like PHP would be necessary.
The dirty solution is one that I'm not even sure would work. The idea is to use a hidden iframe that contains the XML document. Then you could use JavaScript normally to target the hidden frame. However, you wouldn't be updating the file itself because JavaScript doesn't have that kind of power. In reality, this isn't really even a solution then.
Charles
12-06-2008, 07:11 AM
If it's just local and you are using MSIE then you can use Microsoft's extension of JavaScript called JScript or use VB. If you name your page with a .HTA extension the security restrictions go away and you can do pretty much anything. Here are some links to get you started:
http://msdn.microsoft.com/en-us/library/ms536471(vs.85).aspx
http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms763742.aspx
jms318
12-06-2008, 01:13 PM
Thank you RPGFAN and Charles. I love your idea Charles , but the only thing is run a Debian system do using proprietary microsoft stuff is out of the question.
I could use a server side language but then the files would need access to a server and then would not be self contained. I'm trying to keep them self contained if i can.
Would it be possible to write a little JAVA not JS program that i could include and have it use a form to write to a current file on the hard drive?
If not I guess its PHP as RPGFAN recommended which i have no idea to use but gives me good reason to learn it.
Thanks Again for all your help.
Joseph Swager
Charles
12-06-2008, 01:27 PM
Well you need to install and run a local web server. For quick little applications it can't be beat.
http://www.apachefriends.org/en/xampp-linux.html
rpgfan3233
12-06-2008, 01:41 PM
Yeah, you could make use of Java theoretically. All you really need is something that can communicate with the server. If that happens to be a Java servlet page (JSP) or perhaps just a Java program written as a CGI appliation, that would be fine, I'm sure. It's just a matter of what you know.