Capt. Horatio T.P. Webb
Receiving and Creating XML on the Server
Parks -- FALL 2000

The following three methods can be used to get XML to the server:
First an XML object must be created on the server using:

Set object-name= Server.CreateObject("Microsoft.XMLDOM")

Then are three methods to load the XML data:

  1. object-name.load(Request)
    (when receiving an XML file from the client sent by XMLHTTP)
    Click here for an example.

  2. object-name.loadXML(some-string-of-XML)
    (when loading a string to XML)
    Click here for an example.

  3. object-name.load(some-file-of-XML)
    (to retreive XML from a file)
    Click here for an example.


  1. Receiving an xml file in an XMLHTTP client request

    The following asp code sets up an object (xmlReq) to receive the XML file from the client:

     
    Set xmlReq = Server.CreateObject("Microsoft.XMLDOM")
    xmlReq.load(Request)
    if xmlReq.parseError <> 0 then
       Response.write xmlReq.parseError.reason
    end if

     

    The object-name.load(Request) method takes the xml sent from the client using XMLHTTP and loads it to an XMLDOM object.

    Once the XML is loaded to an object, the XML can be parsed. One can now

    1. write it back to the client
    2. save it to SQLServer
    3. write it to a file
    Whatever is done with the XML data, in addition, the server asp must respond to the XMLHTTP request with some form of response. Any response.write content sent back to the client is ok -- xml data, text, whatever.

    Here is an XML file stored as a textarea.

    Here is the response from the server after the data has been sent and loaded on the server:

    Click here to see the source code.
    See also Sending XML from the Client for a discussion of XMLHTTP on the client.

  2. Receiving XML as a form string or querystring or
    Building an XML object from a string

    We can get a string of XML several ways:

    1. A user may return an XML file as a string as part of a request (i.e., a string stored as the value of a name-value pair in a POST or GET request)
    2. We can construct a string of xml in the asp program by building the string logically
    3. We can build the XML string by retreiving data from SQL

    To utilize the XMLDOM to manipulate the tree we must hand the XML string to the XMLDOM (regardless of how we created the XML string). Like this:

     

    xmls="<toptag><first>1</first><second>2</second><third>3</third></toptag>"
    Set xmla = Server.CreateObject("Microsoft.XMLDOM")
    xmla.loadXML(xmls)

     

    Click here to see the result.
    Click here to see the source code.

  3. Loading an XML file stored on the server

    If the XML file is locally stored on the server, you can load the data from the file to the XMLDOM by using:

     

    Set xmlsample = Server.CreateObject("Microsoft.XMLDOM")
    xmlsample.validateOnParse=True
    xmlsample.async=False
    xmlsample.load(Server.MapPath("xml4b.xml"))

     

    Note that the Server.MapPath assume the XML file is stored in the same directory as the asp. Otherwise you must use the full path name (e.g., "c:\somedir\actualfilename.xml").

    Click here to see the source code.
    Click here to see the result.