reference to undeclared namespace prefix: 'drm'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulwagh
    New Member
    • Feb 2013
    • 29

    reference to undeclared namespace prefix: 'drm'

    Please check the below code in child node I want to prefix it with "drm:" but I am getting error reference to undeclared namespace prefix: 'drm'
    Please check the ConvertTexttoXm l procedure
    Code:
    'ForXML
     Dim objDom
     Dim objRoot
     Dim objField
     Dim objFieldValue
     Dim objcolName
     Dim objattTabOrder
     Dim objPI
     Dim objRow
    Sub GenerateXML()
    
        Dim intA As Integer
        Dim intB As Integer
        Dim Sheet1 As String
        Dim ws As Worksheet
       ' Dim shtArray() As String
        Dim shtArrayXML() As String
        Dim Answer As String
        Dim LRow As Integer: LRow = 3
        Dim Row_Max As Integer
        Dim LColumnField() As String
        Dim LColumnFieldValue() As String
       
              
        'Declaration
        Application.ScreenUpdating = False
        Msg10 = "Are you sure you want to Generate XML?"
        ' Asking whether to Generate XML or not
        Answer = MsgBox(Prompt:=Msg10 _
            & vbCr & vbCr & "Click Yes to Generate." _
            & vbCr & vbCr & "Click No to quit.", _
            Title:="Confirmation Message", _
            Buttons:=vbYesNoCancel + vbDefaultButton3)
        'Answer = Confirmation
        If Answer <> vbYes Then
            Exit Sub
        Else
            
            'Call the function to prepare the Output XML file
            PrepareXML (1)
            
                   Workbook_Name = "Metadata Utility.xlsm"
                    totalfields = 0
                       
                    'LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
                    'Column till which nodes have to be created
                    LastColumn = 27
                    For LColumn = 2 To LastColumn
                        ReDim Preserve LColumnField(totalfields + 1)
                    If Cells(5, LColumn).Value <> "" Then
                            LColumnField(totalfields) = Cells(5, LColumn).Value
                            totalfields = totalfields + 1
                    End If
                    Next LColumn
                        
                    totalfields = 0
                  
                    For LColumn = 2 To LastColumn
                        ReDim Preserve LColumnFieldValue(totalfields + 1)
                        LColumnFieldValue(totalfields) = Cells(7, LColumn).Value
                        totalfields = totalfields + 1
                    Next LColumn
                       
                    'Sub to prepare nodes of XML
                    ConvertTexttoXML LColumnField, LColumnFieldValue, totalfields, "Test"
                       
            'Prepare final part of the XML
            PrepareXML (2)
        End If
    End Sub
    
    'Function to prepare XML first and end parts
    Function PrepareXML(xmlPart)
          
    Select Case xmlPart
    
        Case 1
    
                'Instantiate the Microsoft XMLDOM.
                Set objDom = CreateObject("Microsoft.XMLDOM")
                ' Set objDom = CreateObject("MSXML2.DOMDocument.6.0")
                 
                objDom.preserveWhiteSpace = True
        
                 'Create your root element and append it to the XML document.
                 Set objRoot = objDom.createElement("DRMUtility")
                 objDom.appendChild objRoot
                 
                Dim objattTabOrder
                Set objattTabOrder = objDom.createAttribute("xml:space")
                objattTabOrder.Text = "preserve"
                objRoot.setAttributeNode objattTabOrder
    
                
        Case 2
                  'Prepare the XML
                  Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
    
                 objDom.InsertBefore objPI, objDom.ChildNodes(0)
    
                'Send the XML back after adding encoding -ISO-8859-1 to XML tag to read all languages
                 xmlOutPut = Replace(objDom.XML, "?>", " encoding=""ISO-8859-1""?>", 1, 1)
    
    
    
                Set SendDoc = CreateObject("Microsoft.XMLDOM")
                SendDoc.async = False
                SendDoc.LoadXML (xmlOutPut)
               
                Dim FolderName As String
                    With Application.FileDialog(msoFileDialogFolderPicker)
                      .AllowMultiSelect = Faslse
                      .Title = "Choose a folder path to save the file"
                      .Show
                      On Error Resume Next
                          FolderName = .SelectedItems(1)
                          Err.Clear
                      On Error GoTo 0
                    End With
    
                 If FolderName <> "" Then
                        XMLFileName = "DRMUtility"
                        currentTime = Format(Now, "DDMMYYYYhhmmss")
                        currentXMLFile = XMLFileName & "_" & currentTime & ".xml"
                        SendDoc.Save (FolderName & "\" & currentXMLFile)
                     End If
                     
                 Set SendDoc = Nothing
                Set objDom = Nothing
                
    End Select
        
    End Function
    
    'Sub to prepare nodes of XML
    Sub ConvertTexttoXML(OutputFields, OutputVal, totalfields, xmlSheetNodeName)
    
         Dim totalnofields
         Dim i: i = 0
        
         fieldpart = "drm:"
         totalnofields = CInt(totalfields)
         fieldpart = "drm:"
     
        Set objRow = objDom.createElement(xmlSheetNodeName)
        For i = 0 To totalnofields - 1
            Set objField = objDom.createElement(fieldpart & OutputFields(i))
            objField.Text = OutputVal(i)
            objRow.appendChild objField
        Next
    
        objRoot.appendChild objRow
    
    End Sub
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You are in the wrong forum. What language is this? VBA? VB6? VB.Net? Let us know and we can move it to the right forum.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      Rabbit,
      I'm of half a mind to simply delete this...
      • No explanation behind the code.
      • No desciption of the steps taken to troubleshoot over a 100 lines of code.
      • Does not provide the line where the error occured. Doesn't even appear to have looked up what the error means.
      • And it's posted in the wrong forum.

      Comment

      • rahulwagh
        New Member
        • Feb 2013
        • 29

        #4
        It's Excel VBA. Trying to create XML file based on the data in excel and getting the error reference to undeclared namespace prefix: 'drm'.
        The line number where error is throwing is 141.
        I want to add : after drm like <drm:></drm:>
        If I am putting "drm-" then it is working fine but when I am putting "drm:" it is throwing an error.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          Moved your thread to the Access/VBA forum.

          Line 141 -
          Please post the Exact:
          Title.
          Error number.
          Error message.
          Version of Excel you are using.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            The colon is a special character in XML. It denotes that you are using a namespace. Are you trying to use a namespace? If you are, then you need to declare it in the root element. And you need to create your nodes with that namespace. If you are not trying to use a namespace, then the colon character is not allowed.

            Comment

            • rahulwagh
              New Member
              • Feb 2013
              • 29

              #7
              Rabbit, Thanks for your comment.
              Could you please tell me how to namespace and root element so that I can use "drm:".

              Thank in advance.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                You need to use the createNode method that will allow you to specify a namespace.

                Comment

                • rahulwagh
                  New Member
                  • Feb 2013
                  • 29

                  #9
                  Can you please tell me how to write it?

                  Comment

                  • zmbd
                    Recognized Expert Moderator Expert
                    • Mar 2012
                    • 5501

                    #10
                    Why not take a stab at writing the code first and then posting it back for us to tweak if it doesn't work.

                    You'll find that with the exception of the more complex and esoteric bits of code and sql, we prefer to see the work - that we we help keep the poster within the scope of their project. :)

                    Comment

                    • rahulwagh
                      New Member
                      • Feb 2013
                      • 29

                      #11
                      zmbd, no idea how to declare the namespace.
                      Could you please tell me.

                      Comment

                      • zmbd
                        Recognized Expert Moderator Expert
                        • Mar 2012
                        • 5501

                        #12
                        Well, there's theory and application.
                        Giving the code is somewhat not in the spirit of the site, see the FAQ; however, here's a tad of information, I feel that it is very importaint to understand what it is you are trying to do before you attempt the task - maybe that's the Chemist in me, just doing things in the lab without understanding can place people in harms way... not that the code would... but I ramble.

                        Anyway, this is a very short read and you should understand how to do the task: XML namespace - Wikipedia

                        Understanding XML Namespaces
                        From the July 2001 issue of MSDN Magazine.

                        Aaron Skonnard
                        Namespaces are the source of much confusion in XML, especially for those new to the technology. Most of the questions that I receive from readers, students, and conference attendees are related to namespaces in one way or another. It's actually kind of ironic since the Namespaces in XML Recommendation (http://www.w3.org/TR/REC-xml-names) is one of the shorter XML specifications, coming in at just under 10 pages, excluding appendices. The confusion, however, is related to namespace semantics as opposed to the syntax outlined by the specification. To fully understand XML namespaces, you must know what a namespace is, how namespaces are defined, and how they are used. {z}
                        The rest of this column is dedicated to answering these three questions, both syntactically and abstractly. By the time you finish reading this, you'll understand how namespaces affect the family of XML technologies.
                        {z}The emphasis is mine.
                        Last edited by zmbd; Jul 16 '13, 03:25 PM. Reason: [z{added the mag link and quote}]

                        Comment

                        Working...