Reply to this topic  Start new topic
> Writing data to XML file, insert/remove element nodes
AllisterFiend
post Apr 18 2007, 05:40 PM
Post #1






Posts: 19
Joined: 28-January 07
From: Orlando, FL. USA
Member No.: 19,399



I've have figured out how to read from an xml file and I can retrieve the data I need.

My problem seems to be that I can not figure out how to insert some data that someone has entered or remove certain data.

There seems to be a lot of info here if you want to read data from an xml file, but I could not find much on writing or removing data from an xml file. I've been to the unofficial wiki (great tutorial on how to retrieve xml from web), but nothing that I could find on editing a xml file. I have tried various web sites as well as the widget manual (but I cant seem to get all the pieces to work), and after a week I have decided to take it to this forum.

here is an example of my xml file called (xmlFile.xml). This is accessed from my hard drive.

CODE

<?xml version="1.0" encoding="utf-8"?>
<ipData>
    <ipSelection selection="testSelection1">
        <connectionName>TestConnection1</connectionName>
        <ip>111-111-888-888</ip>
        <mask>111-222-888-888</mask>
        <gate>111-333-888-888</gate>
        <dns1>111-444-888-888</dns1>
        <dns2>111-555-888-888</dns2>
        <win1>111-666-888-888</win1>
        <win2>111-777-888-888</win2>
    </ipSelection>
    <ipSelection selection="testSelection2">
        <connectionName>TestConnection2</connectionName>
        <ip>222-111-888-888</ip>
        <mask>222-222-888-888</mask>
        <gate>222-333-888-888</gate>
        <dns1>222-444-888-888</dns1>
        <dns2>222-555-888-888</dns2>
        <win1>222-666-888-888</win1>
        <win2>222-777-888-888</win2>
    </ipSelection>
    <ipSelection selection="testSelection3">
        <connectionName>TestConnection3</connectionName>
        <ip>333-111-888-888</ip>
        <mask>333-222-888-888</mask>
        <gate>333-333-888-888</gate>
        <dns1>333-444-888-888</dns1>
        <dns2>333-555-888-888</dns2>
        <win1>333-666-888-888</win1>
        <win2>333-777-888-888</win2>
    </ipSelection>
</ipData>


I travel to different locations with my labtop and just wanted to make a widget that would allow me to select a certain location and have it change the ip settings on my computer (right now I use batch files, which are not a problem and pretty easy to click on and run), but I figured what the heck, I'll make a widget so I can get some more experience programming as well as learning XML.

I want to be able to enter a new locations name and ip address and have it add that data to the xml file. example below.

CODE

<ipSelection selection="testSelection4">
    <connectionName>TestConnection4</connectionName>
    <ip>444-111-888-888</ip>
    <mask>444-222-888-888</mask>
    <gate>444-333-888-888</gate>
    <dns1>444-444-888-888</dns1>
    <dns2>444-555-888-888</dns2>
    <win1>444-666-888-888</win1>
    <win2>444-777-888-888</win2>
</ipSelection>


I would also like to be able to delete/remove a <ipSelection> </ipSelection> element/node if I no longer need that location listed.

Thanks for any help you can give.

Allister Fiend


edit correction - added sentence that mentions xml file is located on hard drive.




IPB Image
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Drevor
post Apr 18 2007, 06:21 PM
Post #2






Posts: 2,234
Joined: 28-May 05
From: 51.0°N 13.7°E
Member No.: 6,247



Take a look at XML Services Reference.
  • DOMNode insertBefore(DOMNode newChild, DOMNode refChild);
    Inserts newChild before refChild in this node’s children.
  • DOMNode removeChild(DOMNode oldChild);
    Removes oldChild from this node’s children and returns it.
  • DOMNode appendChild(DOMNode newChild);
    Adds the given child node (if this node type allows children).
are the ones that you are looking for


It could look like this:
CODE
var xml=XMLDOM.parse(filesystem.readFile("xmldata.xml"));
var xmldoc=xml.documentElement;
print("\n\nBEFORE\n"+xmldoc.toXML())

xmldoc.removeChild(xmldoc.evaluate("ipSelection[@selection='testSelection3']").item(0));
print("\n\nAFTER REMOVECHILD\n"+xmldoc.toXML())

var newIpSelection=xml.createElement("ipSelection");
newIpSelection.setAttribute("selection","testSelection4");
xmldoc.appendChild(newIpSelection);
var newConnectionName=xml.createElement("connectionName");
newConnectionName.appendChild(xml.createTextNode("TextConnection4"))
newIpSelection.appendChild(newConnectionName);
var newIP=xml.createElement("ip");
newIP.appendChild(xml.createTextNode("333-111-888-888"))
newIpSelection.appendChild(newIP);
//and so on ...
print("\n\nAFTER APPENDCHILD\n"+xmldoc.toXML())


if you have to manage many of those "ipSelection" sets, Id suggest creating a new Object with all these attributes/parameters. Since its all pretty simple content you could actually drop the entire XMLDOM or use it only for reading a new file. After that you could just patch together some strings ...




. . .
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Rob Marquardt
post Apr 18 2007, 06:26 PM
Post #3


I have a spoon.



Posts: 1,817
Joined: 12-February 03
From: California - Bay Area
Member No.: 289



QUOTE(Drevor @ Apr 18 2007, 11:21 AM) *
It could look like this:

Where you had "//and so on ..." I had "// lather, rinse, repeat..." biggrin.gif




User is offlineProfile CardPM
Go to the top of the page
+Quote Post
AllisterFiend
post Apr 19 2007, 11:48 AM
Post #4






Posts: 19
Joined: 28-January 07
From: Orlando, FL. USA
Member No.: 19,399



Thank you Drevor.

Everything works great.


Allister Fiend




IPB Image
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Fast Reply  Reply to this topic    Start new topic  
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: