Adding XML Version node using MSXML and ASP

I need to add the version node ("<?xml version="1.0"?>") for a xml object I'm creating from scratch using ASP and MSXML. I've tried so many times, but I can't seem to get it. It always wants to put it within the first child and not at the very top of the document.

Help! Thanks!

My code...

<%
response.contenttype="text/xml"

'Create an XML DOM object
Set xmlObj = Server.CreateObject("MSXML2.DOMDocument.4.0")

'Create the root element
Set xmlServiceRequest = xmlObj.createElement("RatingServiceSelectionRequest")

      '------------------------ Start Request
      Set xmlRequest = xmlObj.createElement("Request")
      
      Set xmlRequestAction = xmlObj.createElement("RequestAction")
            Set xmlRequestActionText = xmlObj.createTextNode("Rate")
            xmlRequestAction.appendChild(xmlRequestActionText)
            
      Set xmlRequestOption = xmlObj.createElement("RequestOption")
            Set xmlRequestOptionText = xmlObj.createTextNode("Shop")
            xmlRequestOption.appendChild(xmlRequestOptionText)
      
      xmlRequest.appendChild(xmlRequestAction)
      xmlRequest.appendChild(xmlRequestOption)
      
      xmlServiceRequest.appendChild(xmlRequest)
      '------------------------ End Request
      
      '------------------------ Start Pickup Type
      Set xmlPickupType = xmlObj.createElement("PickupType")
      
      Set xmlCode = xmlObj.createElement("Code")
            Set xmlCodeText = xmlObj.createTextNode("01")
            xmlCode.appendChild(xmlCodeText)
            
      xmlPickupType.appendChild(xmlCode)
      
      xmlServiceRequest.appendChild(xmlPickupType)
      '------------------------ End Pickup Type

      '------------------------ Start Shipment
      Set xmlShipment = xmlObj.createElement("Shipment")
      
            '------------------------ Start Shipper
            Set xmlShipper = xmlObj.createElement("Shipper")
                  Set xmlShipperNumber = xmlObj.createElement("ShipperNumber")
                        Set xmlShipperNumberText = xmlObj.createTextNode("########")
                        xmlShipperNumber.appendChild(xmlShipperNumberText)
                  xmlShipper.appendChild(xmlShipperNumber)
                  Set xmlShipperAddress = xmlObj.createElement("Address")
                        Set xmlPostalCode = xmlObj.createElement("PostalCode")
                              Set xmlPostalCodeText = xmlObj.createTextNode("99999")
                              xmlPostalCode.appendChild(xmlPostalCodeText)
                        xmlShipperAddress.appendChild(xmlPostalCode)
                  xmlShipper.appendChild(xmlShipperAddress)
            xmlShipment.appendChild(xmlShipper)
            '------------------------ End Shipper
            
            '------------------------ Start Ship To
            Set xmlShipTo = xmlObj.createElement("ShipTo")
                  Set xmlAddress = xmlObj.createElement("Address")
                        Set xmlPostalCode = xmlObj.createElement("PostalCode")
                              Set xmlPostalCodeText = xmlObj.createTextNode("99999")
                              xmlPostalCode.appendChild(xmlPostalCodeText)
                        xmlAddress.appendChild(xmlPostalCode)
                  xmlShipTo.appendChild(xmlAddress)
            xmlShipment.appendChild(xmlShipTo)
            '------------------------ End Ship To
            
            '------------------------ Start Package
            Set xmlPackage = xmlObj.createElement("Package")
                  'Packaging Type
                  Set xmlPackagingType = xmlObj.createElement("PackagingType")
                        Set xmlCode = xmlObj.createElement("Code")
                              Set xmlCodeText = xmlObj.createTextNode("02")
                              xmlCode.appendChild(xmlCodeText)
                        xmlPackagingType.appendChild(xmlCode)
                  xmlPackage.appendChild(xmlPackagingType)
                  
                  'Package Weight
                  Set xmlPackageWeight = xmlObj.createElement("PackageWeight")
                        Set xmlWeight = xmlObj.createElement("Weight")
                              Set xmlWeightText = xmlObj.createTextNode("25")
                              xmlWeight.appendChild(xmlWeightText)
                        xmlPackageWeight.appendChild(xmlWeight)
                        Set xmlUnitOfMeasurement = xmlObj.createElement("UnitOfMeasurement")
                              Set xmlCode = xmlObj.createElement("Code")
                                    Set xmlCodeText = xmlObj.createTextNode("LBS")
                                    xmlCode.appendChild(xmlCodeText)
                              xmlUnitOfMeasurement.appendChild(xmlCode)
                        xmlPackageWeight.appendChild(xmlUnitOfMeasurement)
                  xmlPackage.appendChild(xmlPackageWeight)

                  'Package Service Options
                  Set xmlPackageServiceOptions = xmlObj.createElement("PackageServiceOptions")
                        Set xmlInsuredValue = xmlObj.createElement("InsuredValue")
                              Set xmlMonetaryValue = xmlObj.createElement("xmlMonetaryValue")
                                    Set xmlMonetaryValueText = xmlObj.createTextNode("325.00")
                                    xmlMonetaryValue.appendChild(xmlMonetaryValueText)
                              xmlInsuredValue.appendChild(xmlMonetaryValue)
                              Set xmlCurrencyCode = xmlObj.createElement("xmlCurrencyCode")
                                    Set xmlCurrencyCodeText = xmlObj.createTextNode("USD")
                                    xmlCurrencyCode.appendChild(xmlCurrencyCodeText)
                              xmlInsuredValue.appendChild(xmlCurrencyCode)
                        xmlPackageServiceOptions.appendChild(xmlInsuredValue)
                  xmlPackage.appendChild(xmlPackageServiceOptions)

            xmlShipment.appendChild(xmlPackage)
            '------------------------ End Package
      
      xmlServiceRequest.appendChild(xmlShipment)
      '------------------------ End Shipment

Response.Write xmlServiceRequest.xml
%>
LVL 1
graphicodysseyAsked:
Who is Participating?
 
rdcproConnect With a Mentor Commented:
<%
response.contenttype="text/xml"

'Create an XML DOM object
Set xmlObj = Server.CreateObject("MSXML2.DOMDocument.4.0")
Set pi = xmlObj.createProcessingInstruction("xml", "version=\"1.0\"")
xmlObj.appendChild pi

'Create the root element
Set xmlServiceRequest = xmlObj.createElement("RatingServiceSelectionRequest")


Or if you want to add one to an existing document, you use:

xmlObj.insertBefore pi, xmlObj.childNodes.item(0)

Regards,
Mike Sharp
0
 
graphicodysseyAuthor Commented:
I tried that first suggestion, and it doesn't add anything. It doesn't produce an error, but it doesn't produce the version node either.

The code I'm trying...

'Create an XML DOM object
Set xmlObj = Server.CreateObject("MSXML2.DOMDocument.4.0")

'Add the processing instructions
Set pi = xmlObj.createProcessingInstruction("xml", "version=""1.0""")
xmlObj.appendChild pi

'Create the root element
Set xmlServiceRequest = xmlObj.createElement("RatingServiceSelectionRequest")

I'll try the second method now.
0
 
graphicodysseyAuthor Commented:
I lied... it did work... well it did after I adjusted something. I wasn't attaching the nodes I was creating back to the DOM object. Once I did that I was able to attach the processing instructions to the DOM. Otherwise the parser was complaining that the root was null. So, my code ended up looking like...

<%
response.contenttype="text/xml"

'Create an XML DOM object
Set xmlObj = Server.CreateObject("MSXML2.DOMDocument.4.0")

'Create the processing instructions node
Set xmlVersion = xmlObj.createProcessingInstruction("xml", "version=""1.0""")

'Create the root element
Set xmlServiceRequest = xmlObj.createElement("RatingServiceSelectionRequest")

'Attach the version and attach the first node
xmlObj.appendChild(xmlVersion)
xmlObj.appendChild(xmlServiceRequest)

...all other node code...

Response.Write xmlObj.xml
%>

Thanks for your help!
0
All Courses

From novice to tech pro — start learning today.