Help me about add node to xml file in javascript!

I have xml file


<?xml version="1.0" encoding="utf-8"?>
<chat>
	<user>HoangThanh:</user>
	<content>Welcome !</content>
</chat>
<chat>
	<user>Guest:</user>
	<content>Hi !</content>
</chat>

I want to add node <chat></chat> with its data <user></user> and <content></content>, how can I do ? Please help me !

Isn’t that invalid XML? As far as I know, an XML document must have a single root element.

If you had one, say,

<?xml version="1.0" encoding="utf-8"?>
<chats>
  <chat>
    <user>HoangThanh:</user>
    <content>Welcome !</content>
  </chat>
  <chat>
    <user>Guest:</user>
    <content>Hi !</content>
  </chat>
</chats>

then you could add another chat element like this:

var chat = document.createElement("chat");
var user = document.createElement("user");
user.appendChild(document.createTextNode("username"));
var content = document.createElement("content");
content.appendChild(document.createTextNode("content"));
chat.appendChild(user);
chat.appendChild(content);
document.getElementsByTagName("chats")[0].appendChild(chat);

(Where document is a reference to the document element of the XML node tree.)

Thanks so much ! But I want it will be appended to xml file ?
I did below, but seems wrong !


function setDB(){
	var chat = document.createElement("chat");
	var user = document.createElement("user");
	user.appendChild(document.createTextNode("username"));
	var content = document.createElement("content");
	content.appendChild(document.createTextNode("content"));
	chat.appendChild(user);
	chat.appendChild(content);
	//document.getElementsByTagName("chats")[0].appendChild(chat);
	
	if (window.ActiveXObject){
		xml = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		xml = new XMLHttpRequest();
	}
	xml.open("GET","db/db.xml",true);
	xml.send(null);
	xml.responseXML.getElementsByTagName("chats")[0].appendChild(chat);
}

please help me ! (and IE8 seems not working :()

Are you retrieving the XML via ajax, and want to append a node to the result?

If so, begin by doing the ajax call. Then use the .responseXML property as the document to which you add the nodes.

var xml;
if (window.ActiveXObject) {
    xml = new ActiveXObject("Microsoft.XMLHTTP");
}  else {
    xml = new XMLHttpRequest();
}
xml.open("GET", "db/db.xml", true);
xml.send(null);

var chat = xml.responseXML.createElement("chat");

var user = xml.responseXML.createElement("user");
user.appendChild(xml.responseXML.createTextNode("username"));

var content = xml.responseXML.createElement("content");
content.appendChild(xml.responseXML.createTextNode("content"));

chat.appendChild(user);
chat.appendChild(content);

xml.responseXML.appendChild(chat);

Are you sure it works ? I did as you said but nothing was added into xml file ! :frowning: Can you help me below ? i only want : when click on button, something will be add into xml file ! I checked on firefox, IE, safari and Chrome, but nothing works !!!


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test AddChild to XML</title>
</head>

<body>
   <input type="button" id="btadd" value="Add" onclick="setDB();"/>
   <script type="text/javascript">
   		var username = "Guest";
		var cntchat = "Hello, world !";
		
		function setDB(){
			var xml;
			if (window.ActiveXObject) {
				xml = new ActiveXObject("Microsoft.XMLHTTP");
			}  else {
				xml = new XMLHttpRequest();
			}
			xml.open("GET", "db.xml", true);
			xml.send(null);
			 
			var chat = xml.responseXML.createElement("chat");
			 
			var user = xml.responseXML.createElement("user");
			user.appendChild(xml.responseXML.createTextNode(username));
			 
			var content = xml.responseXML.createElement("content");
			content.appendChild(xml.responseXML.createTextNode(cntchat));
			 
			chat.appendChild(user);
			chat.appendChild(content);
			 
			xml.responseXML.appendChild(chat);
		}
   </script>
</body>
</html>

I used debug javascript, but no error was found! Something’s wrong ??:confused: I don’t know ! Help me and please test in browser, and tell me your browser, which you used !

What do you mean by ‘xml file’? Are you trying to update a server-side file with JavaScript? In that case, you’ll have to send the augmented XML DOM tree back to the server (through some sort of ajax). You cannot directly modify server-side files with client-side scripting.

And you won’t have access to client-side files, of course. At least not in decent browsers.

There will be no visible result from your code, because you don’t do anything with the XML response after adding the nodes. You just retrieve an XML document via ajax and add a few nodes. Then you do nothing with it.

I think you have to be more clear about what you’re trying to achieve (or wait until someone more clever than me joins in :)). You keep talking about an XML file, but there is no file involved in your sample code.

Meaning I can’t update database with xml ? All databases lay in xml file, how can I update or insert database into xml file as mysql database ?

Not directly with JavaScript, no. You’ll have to send the updated document back to the server and handle it there. You can do that with JavaScript, using ajax, but it seems inefficient.

Why are you doing this with client-side scripting anyway? Shouldn’t all this be handled on the server?

Thanks so much ! Because I only want do small webchat, but host, which supports jsp ( I can do with jsp, ajax and mysql) isn’t free, so that only basic html, xml and script only ! If as you said with script and xml, can’t update database into xml file , then it’s so bad ! :confused:. Thanks so much because very professionally answer me ! And you think with jQuery or xQuery, can they help me ? Perhap I have to buy host, which supports jsp ! Very expensive !!! And can you tell me which javascript editor is best ? Because when I write code (as you write for me) many methods and properties don’t appear (function autocomple code doesn’t work, example - appendChild, createElement…)

Things like jQuery won’t help, because they are merely JavaScript code libraries. You’re still bound by the limitations of what JavaScript is allowed to do, and what a client-side script is able to do.

As for editors, the best one is the one you’re used to. :slight_smile:
This is almost a religious issue with some people, and if you ask 100 developers you’ll probably get 105 different answers.
My personal favourite is vim, but it has a rather steep learning curve if you’re not previously familiar with the vi Unix editor.