SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Location
    Vietnam
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me about add node to xml file in javascript !

    I have xml file
    Code:
    <?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 !

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't that invalid XML? As far as I know, an XML document must have a single root element.

    If you had one, say,
    Code XML:
    <?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:
    Code JavaScript:
    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.)
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Member
    Join Date
    Jun 2009
    Location
    Vietnam
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    Isn't that invalid XML? As far as I know, an XML document must have a single root element.

    If you had one, say,
    Code XML:
    <?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:
    Code JavaScript:
    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 !
    Code:
    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 )

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code JavaScript:
    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);
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Member
    Join Date
    Jun 2009
    Location
    Vietnam
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    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.
    Code JavaScript:
    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 ! 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 !!!
    Code:
    <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 ?? I don't know ! Help me and please test in browser, and tell me your browser, which you used !

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by micheal9000 View Post
    Are you sure it works ? I did as you said but nothing was added into xml file !
    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.
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Member
    Join Date
    Jun 2009
    Location
    Vietnam
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    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 ?

  8. #8
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by micheal9000 View Post
    Meaning I can't update database with xml ?
    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?
    Birnam wood is come to Dunsinane

  9. #9
    SitePoint Member
    Join Date
    Jun 2009
    Location
    Vietnam
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    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 ! . 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...)

  10. #10
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    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.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •