Shoutbox

[Help!] Writing to XML files - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [Help!] Writing to XML files (/showthread.php?tid=62572)

[Help!] Writing to XML files by rob_botch on 07-05-2006 at 11:25 AM

I want to write a script which has one part of its function to log certain messages to an xml file. Each time the script is run, it will add a few tags. for example, <Message>...</Message> for each message. Of course, it will have to have tags around the whole file, such as <Messages>...</Messages>. My query is this: How would I discard the last line of the saved file before writing the new tags? The closing tag would then be added back on.

Thank you,


RE: [Help!] Writing to XML files by -dt- on 07-05-2006 at 11:56 AM

err why not use the XML com object?

code:
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.load("filename of xml here");

//create the element to be added
var toAdd = xml.createElement("test");

//add a message to it
toAdd.appendChild(xml.createTextNode("testing this"));


//add it into our xml
xml.getElementsByTagName("Messages")[0].appendChild(toAdd);


//save our xml
xml.save("filename of xml here");


RE: [Help!] Writing to XML files by rob_botch on 07-05-2006 at 12:52 PM

Thank you for your help.

How would I add more than one child to a parent tag at once. For example, to add the following to an XML file for every message received:

<Message>
          <Sender>Test</Sender>
          <Text>Test Text</Text>
</Message>

Hope you can help,

Edit: I've managed to do this, but the children are always added to the same parent, instead of new ones. How would I retrieve the number of <Message> tags in the file?


RE: [Help!] Writing to XML files by mathieumg on 07-05-2006 at 06:50 PM

code:
var IntMessagesLen = xml.getElementsByTagName("Messages").length; //How many <Message></Message> pairs
xml.getElementsByTagName("Messages")[IntMessagesLen - 1]; //This will do nothing, but you can append any function you want to do on the object (which is the last <Message></Message> pair contained in the xml file.


Hope this helps :)