www.webdeveloper.com
Results 1 to 7 of 7

Thread: changing a XML file in server using Javascript

  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Thumbs up changing a XML file in server using Javascript

    Hello guys, Please I need your advice, I'm very new in XML. I guess it should be easy for your:

    Let's say that we have people.xml with the following structure:
    <people>
    <name country="be"> name1 </name>
    </people>

    I want to have an html (input.html) that allow me to add new names with their country attribute into people.xml using an input tag(form) and javascript.

    But I want that the new names are saved in the people.xml in the server, no in the RAM.

    Please if you can explain me that as for a man of 99 y.o could be great
    Thanks a lot guys.

  2. #2
    Join Date
    Dec 2011
    Posts
    74
    XML isn't really the issue here, it's whatever language you choose to generate the XML file. The XML itself is arbitrary.

    As you have mentioned the use of javascript: I don't think this is the way to go. As you wish to save to server you will require a serverside language such as ASP.NET or PHP.

  3. #3
    Join Date
    Jun 2012
    Posts
    3
    Thanks a lot for your answer Daniel.

  4. #4
    Join Date
    Nov 2007
    Posts
    432
    I recently built a site for a client that uses an XML file for some of the content items.

    Along with the site, I built an "admin" page using Classic ASP, XML, and XSL that allows them to update the XML file (including being able to add additional records).

    The basic design is like this:
    1- "admin.asp" - reads the XML file and presents the data in an HTML form using a table for the layout.
    2- user makes updates, adds rows, etc.
    3- then the user clicks "Save" the form posts to a page which loops through all of the table rows, creating XML as it goes, ultimately overwriting the current XML file with a new one.

    The function that creates the XML, looks like this:
    Code:
    function updateFile(xmlfile)
    	newProductList = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>" & vbCRLF
    	newProductList = newProductList & "<Doc>" & vbCRLF
    	numberofRecords = Request.Form("numberOfRecords")
    
    	i = 1
    	For i = 1 to numberOfRecords
    		If validRow(i) Then
    			newProductList = newProductList & "	<Product id=""" & i & """>" & vbCRLF
    			newProductList = newProductList & "		<Type>" & Request.Form("Type_"&i) & "</Type>" & vbCRLF
    			newProductList = newProductList & "		<ProductName>" & Request.Form("ProductName_"&i) & "</ProductName>" & vbCRLF
    			newProductList = newProductList & "		<Unit>" & Request.Form("unit_"&i) & "</Unit>" & vbCRLF
    			newProductList = newProductList & "		<Price>" & Request.Form("Price_"&i) & "</Price>" & vbCRLF
    			newProductList = newProductList & "		<Image>" & Request.Form("Image_"&i) & "</Image>" & vbCRLF
    			newProductList = newProductList & "		<Description>" & Request.Form("Description_"&i) & "</Description>" & vbCRLF
    			newProductList = newProductList & "	</Product>" & vbCRLF
    		End If
    	Next
    
    	newProductList = newProductList & "</Doc>"
    	WriteToFile server.MapPath("data\product_list.xml"), newProductList, false
    end function
    The function "WriteToFile" looks like this:
    Code:
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    function WriteToFile(FileName, Contents, Append)
    	
    	if Append = true then
    	   iMode = 8
    	else 
    	   iMode = 2
    	end if
    
    	set oFs = server.createobject("Scripting.FileSystemObject")
    	set oTextFile = oFs.OpenTextFile(FileName, iMode, True)
    	oTextFile.Write Contents
    	oTextFile.Close
    	set oTextFile = nothing
    	set oFS = nothing
    
    end function

  5. #5
    Join Date
    Jun 2012
    Posts
    3
    THanks a lot for your response nap0leon

    I guess I have to analyse it pretty calm because I'm new using XML... I will copy paste your codes in some blank pages so that I will see what they show me.

    Can I use 1 xml file to work with different domains hosted in different servers?

  6. #6
    Join Date
    Nov 2007
    Posts
    432
    The code just writes a "string" to a file, the string happens to be in XML format, to whatever file you point it to. Since this runs server-side, if you want the process to update files on different servers, you would need to send the information to a web page on each server.

  7. #7
    Join Date
    Jun 2013
    Posts
    1
    Quote Originally Posted by nap0leon View Post
    I recently built a site for a client that uses an XML file for some of the content items.

    Along with the site, I built an "admin" page using Classic ASP, XML, and XSL that allows them to update the XML file (including being able to add additional records).

    The basic design is like this:
    1- "admin.asp" - reads the XML file and presents the data in an HTML form using a table for the layout.
    2- user makes updates, adds rows, etc.
    3- then the user clicks "Save" the form posts to a page which loops through all of the table rows, creating XML as it goes, ultimately overwriting the current XML file with a new one.

    The function that creates the XML, looks like this:
    Code:
    function updateFile(xmlfile)
    	newProductList = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>" & vbCRLF
    	newProductList = newProductList & "<Doc>" & vbCRLF
    	numberofRecords = Request.Form("numberOfRecords")
    
    	i = 1
    	For i = 1 to numberOfRecords
    		If validRow(i) Then
    			newProductList = newProductList & "	<Product id=""" & i & """>" & vbCRLF
    			newProductList = newProductList & "		<Type>" & Request.Form("Type_"&i) & "</Type>" & vbCRLF
    			newProductList = newProductList & "		<ProductName>" & Request.Form("ProductName_"&i) & "</ProductName>" & vbCRLF
    			newProductList = newProductList & "		<Unit>" & Request.Form("unit_"&i) & "</Unit>" & vbCRLF
    			newProductList = newProductList & "		<Price>" & Request.Form("Price_"&i) & "</Price>" & vbCRLF
    			newProductList = newProductList & "		<Image>" & Request.Form("Image_"&i) & "</Image>" & vbCRLF
    			newProductList = newProductList & "		<Description>" & Request.Form("Description_"&i) & "</Description>" & vbCRLF
    			newProductList = newProductList & "	</Product>" & vbCRLF
    		End If
    	Next
    
    	newProductList = newProductList & "</Doc>"
    	WriteToFile server.MapPath("data\product_list.xml"), newProductList, false
    end function
    The function "WriteToFile" looks like this:
    Code:
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    function WriteToFile(FileName, Contents, Append)
    	
    	if Append = true then
    	   iMode = 8
    	else 
    	   iMode = 2
    	end if
    
    	set oFs = server.createobject("Scripting.FileSystemObject")
    	set oTextFile = oFs.OpenTextFile(FileName, iMode, True)
    	oTextFile.Write Contents
    	oTextFile.Close
    	set oTextFile = nothing
    	set oFS = nothing
    
    end function
    Hey I kind of just stumbled across this while working on a site. I'm trying to create a page similar to yours, with an admin page where a user can edit a list that gets displayed on the page. The is a set of people and is stored in an xml file with their name, personal webpage, and their position on our team.
    I have experience programming, but I've never worked with asp xml or xls, and im still a student, so I'm not even sure how to get started. Any help would be appreciated.

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