Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 5th, 2005, 02:45 AM
Registered User
 
Join Date: Sep 2004
Location: , , USA.
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.
Reply With Quote
  #2 (permalink)  
Old January 5th, 2005, 05:56 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:14 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.