Posting XML Data From the Client to a Server Eugenia Fernandez IUPUI
Client Server XML Example 1. Read catalog 5. Update database 2. Catalog XML 4. Order XML 3. Manipulate XML & build XML data packet Data Source Web Server Client
Why Bother? XML data is manipulated on the client with changes made at the client which reduces the load on the server. Only one update and only the updated information need be sent back to the server. All transfers are done in XML text which are small in size.
Sending XML from Client to Server 1. Client populates a DOMDocument object with XML data to send to the server. 2. Client creates XMLHTTP object, and loads the XML data packet into the XMLHTTP object. 3. Client sends the data to an ASP page at the server. 4. Server executes the ASP page and creates a server-side DOMDocument object. 5. The ASP loads the XML data packet into the server-side DOMDocument object. 6. The ASP processes the data as needed.
Building an XML Data Packet Create a DOMDocument object to hold the XML data from any of the following sources: XML data island at the client a stand-alone XML document a dynamically generated XML document containing a mixture of boilerplate XML, data from XML data islands, and data created from user input at the client
Example: Adding Existing XML Data Island into a DOM Tree 1. Create a DOMDocument object set docSubmit = CreateObject(“MSXML2.DOMDocument”) docSubmit.async = false 2. Access the XML data island set docOrder = xmldso.XMLDocument set nodeOrder = docOrder.selectSingleNode(“//order”)
Example: Adding Existing XML Data Island into a DOM Tree 2 3. Create a copy of the XML data. set sendOrder = nodeOrder.cloneNode(true) The node you obtained is part of an existing DOM tree. The appendChild method will remove the node from the tree it is currently in before adding it to the new DOM tree. Thus you must create a copy of the node using the cloneNode method. The cloneNode method takes a single parameter indicating whether child elements are included in the copy. 4. Add the copied node to the XML data packet docSubmit.documentElement.appendChild sendOrder
Create the XMLHTTP Object Create a MSXML2.XMLHTTP object Open the request, specifying Delivery method, url, async flag UserID and password (if needed) Set poster = CreateObject(“MSXML2.XMLHTTP”) poster.open “POST”, “CustomerOrder.asp”, False
Sending Data to the Server Send the XML data poster.send docSubmit Check the readyState property of the Document, which tells if the server has built the Response object
XMLHTTP Properties Syntax poster.Open http-method, url, [async, userID, pw] Parameters http-method: GET or POST url: the url that will receive the XML data at the server, typically an ASP or CGI page async: True or False indicating whether the request is asynchronous. If asynchronous (true) the client will not wait for a response when sending the data. If synchronous (false), the client waits for a response before continuing. userID: the userID, if any, required for authentication at the server pw: the password, if any, required for authentication at the server
ReadyState Values Can use this value in a script to check state of document state = doc.readyState Can also use onReadyStateChange property to check state whenever it changes and inform client of progress doc.onReadyStateChange = GetRef(“CheckState”) ValueMeaning 0Response object is created, but not yet loaded 1Document is being loaded 2Document has been loaded and parsing is underway 3Parsing is partially done so partial data is available to the client 4Document has been completely parsed and is fully available to the client
Loading XML Data on the Server Create a DOMDocument object set docReceived = CreateObject(“MSXML2.DOMDocument”) Load the XML data from the Request object docReceived.async = false docReceived.load Request Access XML data in the DOMDocument object set rootNode = docReceived.documentElement
Manipulating XML at the Server Once an XML document has been loaded into a DOMDocument object, you can use DOM to access and manipulate the XML data. For example, the following ASP script obtains a collection of all elements in a document and uses a loop to navigate through the collection set listBookOrder = docReceived.selectNodes(“//orderitem”) for each node in listBookOrder ‘ process a node (representing a bookOrder element) Next
Updating the Database Open an ADO connection to the database Write ASP script to query and update the database as needed, e.g. insert new record. set listBookOrder = docReceived.selectNodes(“//orderitem”) for each node in listBookOrder ‘get details for this node & insert new record isbn = node.getAttribute(“isbn”) price = node.getAttribute(“price”) qty = node.getAttribute(“qty”) connName.execute (“Insert into orderItems VALUES ” &_ isbn & “,” & price & “, “ & qty) Next
Building an Response Response can be built in many ways Plain Text HTML page XML document HTML page with an XML data island
Building an HTML Response Example Confirmation of Order <% for each node in listOrderItem title = node.getAttribute(“title”) set quantityNode = node.selectSingleNode(“quantity”) quantity = quantityNode.firstChild.nodeValue %>,
Responding with Static XML set docResponse = CreateObject(“MSXML2.DOMDocument”) docResponse.async = false docResponse.load “MyFixedResponse.xml” Response.ContentType = “text/xml” Response.Save docResponse
Retrieving an HTML Response Use the responseText property of XMLHTTP Assign the response string to an HTML element Example Set poster = CreateObject(“MSXML2.XMLHTTP”) poster.open “POST”, “CustomerOrder.asp”, False poster.send docSubmit displayArea.innerHTML = poster.responseText
Retrieving an XML Response Use the responseXML property of XMLHTTP Manipulate using DOM Example Set poster = CreateObject(“MSXML2.XMLHTTP”) poster.open “POST”, “CustomerOrder.asp”, False poster.send docSubmit set docResponse = poster.responseXML set listItem = docResponse.selectNodes(“//item”)
Source “Building XML-Based Web Applications” a Microsoft Certified Course.