Solved

Adding XML Version node using MSXML and ASP

Posted on 2004-10-27
466 Views
Last Modified: 2008-01-09
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
%>
0
Question by:graphicodyssey
    3 Comments
     
    LVL 26

    Accepted Solution

    by:
    <%
    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
     
    LVL 1

    Author Comment

    by:graphicodyssey
    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
     
    LVL 1

    Author Comment

    by:graphicodyssey
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Suggested Solutions

    The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
    Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now