• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding nodes to an XML file

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

I have an xml file Users.xml

<?xml version .....?>
<users>
<user>
<cid>101</cid>
<name>poonam</name>
</user>
</users>

I've written the following code to add a user node , but it's not working.
What is wrong with the code?

<html>
<head>
<script type="text/javascript">
var name,cid;
var xmlDoc;
function add(form1)
{
name=form1.name.value;
cid=form1.cid.value;
loadXML();
}

function loadXML()
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("Users.xml");
parseXML();
}

function parseXML()
{
var x=xmlDoc.documentElement;
alert(x.nodeName+"nodename");
var newNode=xmlDoc.createElement("user");
var newCid=xmlDoc.createElement("cid");
var newName=xmlDoc.createElement("name");
var newcidText=xmlDoc.createTextNode(cid);
var newnameText=xmlDoc.createTextNode(name);

newCid.appendChild(newcidText);

newName.appendChild(newnameText);

newNode.appendChild(newCid);
newNode.appendChild(newName);
x.appendChild(newNode);
//xmlDoc.appendChild(x);

}

</script>
</head>
<body>
<form onsubmit="add(this)">
<center>Name : <input type="text" name="name" size="50"></center>
<center>CID : <input type="text" name="cid" size="50"></center>
<center><input type="submit" value ="go"></center>
</form>
</body>
</html>
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but it's not working.



At what point do you determine that it is "not working"? Does the host computer explode, freeze up, turn to dust

In other words, what happens when you try it?

Bill
[ July 06, 2007: Message edited by: William Brogden ]
 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've written the code to add a user node in Users.xml
The code is supposed to take the values of "cid" and "name" from the html
page and add those to the Users.xml
But after execution no node is added to the Users.xml

poonam K.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But after execution no node is added to the Users.xml



Thats probably because you have only modified the DOM in memory. The Users.xml remains unchanged because you have not written the modified DOM over it. This section of Sun's tutorial should get you started.

Bill
 
Whatever. Here's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic