Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2005, 02:45 AM
Registered User
 
Join Date: Sep 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to TheNinthPlayer Send a message via MSN to TheNinthPlayer
Default Javascript and using the XMLDOM to modify xml

I created a script which loads my xml file and it also saves my xml file to a persons HD using an ActiveX plugin.

Now what I would like to do is have my script take the loaded XML file and add child elements and then save it. But I want this to be done in memory that way the user doesnt have to see it.


So here is what the load part looks like:
Code:
<script type="text/javascript">
  <!--
  // Loads plugins.xml
  var xmlFile = './plugins.xml';

  function loadXML(xmlFile) {
    var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');

    xmlDoc.async = false;
    xmlDoc.onreadystatechange = verify(xmlDoc);
    xmlDoc.load(xmlFile);
    xmlObj = xmlDoc.documentElement;

    return xmlDoc;
  }

  function verify(xmlDoc) {
    // 0 Object is not initialized
    // 1 Loading object is loading data
    // 2 Loaded object has loaded data
    // 3 Data from object can be worked with
    // 4 Object completely initialized

    if (xmlDoc.readyState != 4) {
      return false;
    }

    return xmlDoc;
  }

  function getPlugins(xmlFile) {
    loadXML(xmlFile);

    var pluginFiles = Array();
    var nodeCount = xmlObj.childNodes.length;

    for( var x = 0; x < nodeCount; x++) {
      pluginFiles[x] = xmlObj.getElementsByTagName("plugin")[x].childNodes(1).text;
    }

    for( var x = 0; x < pluginFiles.length; x++) {
      document.write("      <script type=\"text/javascript\" src=\"./plugins/" + pluginFiles[x] + "\"></script>\n");
    }

    return true;
  }

  getPlugins(xmlFile);
   //-->
 </script>


I imagine I should be using the 'xmlObj' I created to add child nodes but I am unsure how to do this. Anyone have any suggestions?

Once that part is complete I have a script made to save the current xmlObj to a file.

And this is my XML file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<plugins>
  <plugin>
    <name>Music</name>
    <file>music.js</file>
  </plugin>
  <plugin>
    <name>Weather</name>
    <file>weather.js</file>
  </plugin>
  <plugin>
    <name>Notes</name>
    <file>notes.js</file>
  </plugin>
</plugins>
I want to add a new 'plugin' node with its nessacary child nodes.
 
Old January 5th, 2005, 05:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Firstly you need to capture the return of loadXML:
Code:
var oDoc = loadXML(xmlFile);
Then work with oDoc as your ML document. To load and save from the hard drive the client will need to have low security settings for your site's zone.

If you document.write to a page that has finished loading it will create a new page, better to create a new script node and append to the current document.



--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify XML with XSLT aowen355 XSLT 4 December 26th, 2007 10:19 AM
JavaScript and XMLDOM kirthi97 XML 4 June 27th, 2006 02:22 AM
re: modify the column in a xml file ramya General .NET 1 January 20th, 2005 04:50 AM
Parsing xml datastream using XMLDOM realgone_ XML 8 September 23rd, 2004 08:50 AM
XML, XML Schema, JavaScript, ASP cyberjames2003 XML 0 June 4th, 2003 04:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.