www.webdeveloper.com
Results 1 to 3 of 3

Thread: Append XML node in javascript

Threaded View

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Append XML node in javascript

    I have a callback function that returns an xml string which is a table of routes and corresponding miles.
    Code:
    xmlData=new ActiveXObject("Microsoft.XMLDOM");
    xmlData.async="false";
    xmlData.loadXML(returnmessage);
    the xmlData.xml is this:
    Code:
    <NewDataSet>
    	<Routes>
    		<RouteName>StLouisToNewYork</RouteName>
    		<Miles>556</Miles>
    	</Routes>
    	<Routes>
    		<RouteName>MinneapolisToDallas</RouteName>
    		<Miles>948</Miles>
    	</Routes>
    	<Routes>
    		<RouteName>LosAngelesToDenver</RouteName>
    		<Miles>1033</Miles>
    	</Routes>
    </NewDataSet>
    In javascript, I would like to add a new route to my xmlData object.
    Code:
    var strRte = "DenverToAtlanta";
    var strMi = "1408";
    How do I do this? This is what I'm trying, but I'm throwing an error.
    Code:
    var newRoute = xmlData.createElement("Routes");
    var nameNode = newRoute.createElement("RouteName");  <<<<<<<< Object doesn't support this property or method
    var nameText = nameNode.createTextNode(strRte);
    var milesNode = newRoute.createElement("Miles");
    var milesText = milesNode.createTextNode(strMi);
    newRoute.appendChild(nameNode);
    newRoute.appendChild(milesNode);
    xmlData.appendChild(newRoute);
    I want my xmlData.xml to be this (Although I don't care which position):
    Code:
    <NewDataSet>
    	<Routes>
    		<RouteName>StLouisToNewYork</RouteName>
    		<Miles>556</Miles>
    	</Routes>
    	<Routes>
    		<RouteName>MinneapolisToDallas</RouteName>
    		<Miles>948</Miles>
    	</Routes>
    	<Routes>
    		<RouteName>LosAngelesToDenver</RouteName>
    		<Miles>1033</Miles>
    	</Routes>
    	<Routes>
    		<RouteName>DenverToAtlanta</RouteName>
    		<Miles>1408</Miles>
    	</Routes>
    </NewDataSet>
    Last edited by Kor; 07-01-2010 at 05:41 AM. Reason: wrap the code [code][/code]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles