none
Question on processing instructions targeted at XML RRS feed

  • Question

  • I have this code to set the processing instructions for my XML:

        ' Create a processing instruction targeted for xml.
        dom.appendChild dom.createProcessingInstruction("xml", _
            "version='1.0' encoding='UTF-8'")

    I need my XML to look like this:

      <?xml version="1.0" encoding="UTF-8" ?> 
    - <InUseVerificationProgramSubmission xmlns="http://www.exchangenetwork.net/schema/verify/iuvp/1">
        <SubmissionAuthorDetails>
          <EPAManufacturerCode>MBX</EPAManufacturerCode> 

    However, my code mentioned at the top adds "xmlns=''" after every child node (i.e. SubmissionAuthorDetails). How do I set the processing instructions so that it looks like the way I need it above?

    Here is what my own code spits out, notice the extra 'xmlns' after SubmissionAuthorDetails:

      <?xml version="1.0" encoding="UTF-8" ?> 
    - <InUseVerificationProgramSubmission xmlns="'http://www.exchangenetwork.net/schema/verify/iuvp/1'">
        <SubmissionAuthorDetails xmlns="">
          <EPAManufacturerCode>MBX</EPAManufacturerCode> 

    Wednesday, November 28, 2012 3:49 PM

Answers

  • Mar Jan has given a solution in a new way.Here I am trying to update the earlier code which was given as soution.

    Actually The problem is in MSXML.It does not allow the method CreateElementNS by which you can specify a namespace.

    https://msmvps.com/blogs/martin_honnen/archive/2009/04/15/creating-xml-with-namespaces-with-javascript-and-msxml.aspx

    As we are giving namespace to root but not children,parser assumes that children not within same namespace and overrides by xmlns="".

    A solution in different way is given below.I have found some link that if INternet Explorer 9 or above installed then the above method is available.But the following will work in older version.

    Private Sub ExportXMLCB_Click()
        
        Dim dom As DOMDocument50
        Dim node As IXMLDOMNode
        Dim cnode As IXMLDOMNode
        Dim dnode As IXMLDOMNode
        Dim root As IXMLDOMNode
        Dim att As IXMLDOMNode
        
        Dim ns As String
            
        Set dom = CreateDOM
        
        ns = "'http://www.exchangen...a/verify/iuvp/1'"
        
        ' Create a processing instruction targeted for xml.
        dom.appendChild dom.createProcessingInstruction("xml", _
            "version='1.0' encoding='UTF-8'")
        
        ' Create the root element and add attribute
       
        Set root = dom.createNode("element", "InUseVerificationProgramSubmission", ns)
            
        dom.appendChild root
        
            'Adding 1st child of root
            Set cnode = dom.createNode("Element", "SubmissionAuthorDetails", ns)
            root.appendChild cnode
        
                'Adding grand childs
                
                'EPAManufacturerCode
                Set node = dom.createNode("Element", "EPAManufacturerCode", ns)
                node.Text = "MBX"
                
                cnode.appendChild node
                
                'SubmissionAuthorUserID
                Set node = dom.createNode("Element", "SubmissionAuthorUserID", ns)
                node.Text = "CETNAR712"
                cnode.appendChild node
                
                'SubmissionAuthorUserID
                Set node = dom.createNode("Element", "SubmissionAuthorUserID", ns)
                node.Text = "CETNAR712"
                cnode.appendChild node
                
                'SubmissionAuthorFullName
                Set node = dom.createNode("Element", "SubmissionAuthorFullName", ns)
                node.Text = "Mr Charles Cetnar"
                cnode.appendChild node
                
                'SubmissionAuthorEmailAddress
                Set node = dom.createNode("Element", "SubmissionAuthorEmailAddress", ns)
                node.Text = "charles.cetnar@daimler.com"
                cnode.appendChild node
                
                'SubmissionAuthorPhoneNumber
                Set node = dom.createNode("Element", "SubmissionAuthorPhoneNumber", ns)
                node.Text = "charles.cetnar@daimler.com"
                cnode.appendChild node
                
                'SubmissionSubmitterUserID
                Set node = dom.createNode("Element", "SubmissionSubmitterUserID", ns)
                node.Text = "CETNAR712"
                cnode.appendChild node
                
                'SubmissionParameterDetails
                
                    'Adding 1st child (dnode) to ParameterDetails(node)
                    Set node = dom.createNode("Element", "SubmissionParameterDetails", ns)
                    Set dnode = dom.createNode("Element", "SubmissionParameterKey", ns)
                    dnode.Text = "CARBFileName"
                
                    node.appendChild dnode
                
                    'Adding 2nd child (dnode) to ParameterDetails(node)
                    Set dnode = dom.createNode("Element", "SubmissionParameterText", ns)
                    dnode.Text = "MBX_IUVP_20121108093549.xml"
                    node.appendChild dnode
                    
                    'Adding Parameter details to cnode
                    cnode.appendChild node
        
        ' Save the XML document to a file.
        dom.Save (ThisWorkbook.Path & "\test.xml")
        
        'Housekeeping
        Set root = Nothing
        Set dom = Nothing
        MsgBox ("XML export complete.")
    
    End Sub



    Best Regards,
    Asadulla Javed, Kolkata
    ---------------------------------------------------------------------------------------------
    Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it solves the issue.

    • Marked as answer by Peterc13 Monday, December 10, 2012 2:41 PM
    Saturday, December 1, 2012 8:25 AM
    Answerer

All replies

  • bump..

    Wednesday, November 28, 2012 7:40 PM
  • Hi Peterc13,

    Based on some sample code I have (watch out for line wrap in this post):

    Sub Foo()
            Dim oDom As MSXML2.DOMDocument
            Dim oDOMPI As MSXML2.IXMLDOMProcessingInstruction
            Dim sXML As String
            Set oDom = New MSXML2.DOMDocument
            Set oDOMPI = oDom.createProcessingInstruction("xml", "version='1.0'
     Encoding='UTF-8'")
            oDom.appendChild oDOMPI
            Set oDOMPI = Nothing
            sXML = "<InUseVerificationProgramSubmission xmlns=""http://www.exchangenetwork.net/schema/verify/iuvp/1"">"
            sXML = sXML & "<SubmissionAuthorDetails>"
            sXML = sXML & "<EPAManufacturerCode>MBX</EPAManufacturerCode>"
            sXML = sXML & "</SubmissionAuthorDetails>"
            sXML = sXML & "</InUseVerificationProgramSubmission>"
            oDom.loadXML oDom.XML & sXML
            MsgBox oDom.XML
    End Sub

    Regards, Jan Karel Pieterse|Excel MVP|http://www.jkp-ads.com
    Thursday, November 29, 2012 7:56 AM
  • This is helpful however, only if that is the entirety of the xml document that I need to create. The snippet I posted of the xml code is only the first portion. It actually is a very large xml file. Thank you for your help and understanding as I am still new to this entire language. Learning though!

    Here is a better idea of the code I'm using, however, still just the beginning portion:

    Private Function CreateDOM() Dim dom Set dom = New DOMDocument60 dom.async = False dom.validateOnParse = False dom.resolveExternals = False dom.preserveWhiteSpace = True Set CreateDOM = dom End Function

    Public Sub ExportXMLCB_Click() Dim dom As DOMDocument60 ' node = grandchild node Dim node As IXMLDOMNode ' cnode = child node Dim cnode As IXMLDOMNode ' dnode = greatgrandchild node Dim dnode As IXMLDOMNode Dim root As IXMLDOMNode Dim att As IXMLDOMAttribute Set dom = CreateDOM ' Create a processing instruction targeted for xml. dom.appendChild dom.createProcessingInstruction("xml", _ "version='1.0' encoding='UTF-8'") ' Create the root element and add attribute Set root = dom.createElement("InUseVerificationProgramSubmission") Set att = dom.createAttribute("xmlns") att.NodeValue = "'http://www.exchangenetwork.net/schema/verify/iuvp/1'" root.Attributes.setNamedItem att dom.appendChild root Set att = Nothing

    I just need to get rid of that pesky "xmlns=" on every child node. Thank you for your help!
    Thursday, November 29, 2012 1:27 PM
  • Hi Peterc13,

    What I was trying to suggest is to build the entire xml in a string and then in the end load the entire xml into the dom. That effectively gives you all control over the exact appearance of your XML. SO your code would be something like:

    Sub Foo()
        Dim oDOM As MSXML2.DOMDocument
        Dim oDOMPI As MSXML2.IXMLDOMProcessingInstruction
        Dim sXML As String

        Set oDOM = New MSXML2.DOMDocument
        Set oDOMPI = oDOM.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
        oDOM.appendChild oDOMPI
        Set oDOMPI = Nothing
        sXML = "<InUseVerificationProgramSubmission xmlns=""http://www.exchangenetwork.net/schema/verify/iuvp/1"">"
        For Each Author In Authors
            sXML = sXML & "<SubmissionAuthorDetails>"
            For Each Manufacturer In Manufacturers
                sXML = sXML & "<EPAManufacturerCode>" & Manufacturer.Value & "</EPAManufacturerCode>"
            Next
            sXML = sXML & "</SubmissionAuthorDetails>"
        Next
        sXML = sXML & "</InUseVerificationProgramSubmission>"
        oDOM.LoadXML oDOM.XML & sXML
    End Sub


    Regards, Jan Karel Pieterse|Excel MVP|http://www.jkp-ads.com
    Thursday, November 29, 2012 2:43 PM
  • Mar Jan has given a solution in a new way.Here I am trying to update the earlier code which was given as soution.

    Actually The problem is in MSXML.It does not allow the method CreateElementNS by which you can specify a namespace.

    https://msmvps.com/blogs/martin_honnen/archive/2009/04/15/creating-xml-with-namespaces-with-javascript-and-msxml.aspx

    As we are giving namespace to root but not children,parser assumes that children not within same namespace and overrides by xmlns="".

    A solution in different way is given below.I have found some link that if INternet Explorer 9 or above installed then the above method is available.But the following will work in older version.

    Private Sub ExportXMLCB_Click()
        
        Dim dom As DOMDocument50
        Dim node As IXMLDOMNode
        Dim cnode As IXMLDOMNode
        Dim dnode As IXMLDOMNode
        Dim root As IXMLDOMNode
        Dim att As IXMLDOMNode
        
        Dim ns As String
            
        Set dom = CreateDOM
        
        ns = "'http://www.exchangen...a/verify/iuvp/1'"
        
        ' Create a processing instruction targeted for xml.
        dom.appendChild dom.createProcessingInstruction("xml", _
            "version='1.0' encoding='UTF-8'")
        
        ' Create the root element and add attribute
       
        Set root = dom.createNode("element", "InUseVerificationProgramSubmission", ns)
            
        dom.appendChild root
        
            'Adding 1st child of root
            Set cnode = dom.createNode("Element", "SubmissionAuthorDetails", ns)
            root.appendChild cnode
        
                'Adding grand childs
                
                'EPAManufacturerCode
                Set node = dom.createNode("Element", "EPAManufacturerCode", ns)
                node.Text = "MBX"
                
                cnode.appendChild node
                
                'SubmissionAuthorUserID
                Set node = dom.createNode("Element", "SubmissionAuthorUserID", ns)
                node.Text = "CETNAR712"
                cnode.appendChild node
                
                'SubmissionAuthorUserID
                Set node = dom.createNode("Element", "SubmissionAuthorUserID", ns)
                node.Text = "CETNAR712"
                cnode.appendChild node
                
                'SubmissionAuthorFullName
                Set node = dom.createNode("Element", "SubmissionAuthorFullName", ns)
                node.Text = "Mr Charles Cetnar"
                cnode.appendChild node
                
                'SubmissionAuthorEmailAddress
                Set node = dom.createNode("Element", "SubmissionAuthorEmailAddress", ns)
                node.Text = "charles.cetnar@daimler.com"
                cnode.appendChild node
                
                'SubmissionAuthorPhoneNumber
                Set node = dom.createNode("Element", "SubmissionAuthorPhoneNumber", ns)
                node.Text = "charles.cetnar@daimler.com"
                cnode.appendChild node
                
                'SubmissionSubmitterUserID
                Set node = dom.createNode("Element", "SubmissionSubmitterUserID", ns)
                node.Text = "CETNAR712"
                cnode.appendChild node
                
                'SubmissionParameterDetails
                
                    'Adding 1st child (dnode) to ParameterDetails(node)
                    Set node = dom.createNode("Element", "SubmissionParameterDetails", ns)
                    Set dnode = dom.createNode("Element", "SubmissionParameterKey", ns)
                    dnode.Text = "CARBFileName"
                
                    node.appendChild dnode
                
                    'Adding 2nd child (dnode) to ParameterDetails(node)
                    Set dnode = dom.createNode("Element", "SubmissionParameterText", ns)
                    dnode.Text = "MBX_IUVP_20121108093549.xml"
                    node.appendChild dnode
                    
                    'Adding Parameter details to cnode
                    cnode.appendChild node
        
        ' Save the XML document to a file.
        dom.Save (ThisWorkbook.Path & "\test.xml")
        
        'Housekeeping
        Set root = Nothing
        Set dom = Nothing
        MsgBox ("XML export complete.")
    
    End Sub



    Best Regards,
    Asadulla Javed, Kolkata
    ---------------------------------------------------------------------------------------------
    Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it solves the issue.

    • Marked as answer by Peterc13 Monday, December 10, 2012 2:41 PM
    Saturday, December 1, 2012 8:25 AM
    Answerer