Extracting A Tag Element Value From An XML Document

Posted on 2006-06-01
Last Modified: 2013-11-19
I am trying to extract a single element from an xml document.    I would like the record_no value. I am using the singlenode and typedvalue to extract but have no luck.  The root element is starting at error. Set objNodes = objXMLDOM.selectNodes("/IBM/List_Wrapper/_bp").  Using VBSCript and DOM.  Tried it several times with no luck.  

- <IBM>
- <error>
  <message>(407)Proxy Authentication Required</message>
  <_bpname>Purchase Orders</_bpname>
- <List_Wrapper>
- <_bp>
   <record_no />
Question by:RVattakunntel
    LVL 60

    Expert Comment

    by:Geert Bormans
    Hi RVattakunntel,

    by any chance an xmlns attribute higher up in the file?


    Author Comment

    No. IBM is the root element
    LVL 26

    Expert Comment



    Set objNode = xmlDoc.selectSingleNode("/IBM/List_Wrapper/_bp/record_no")

    selectNodes will select a nodeset, that you have to iterate over.  If you want just a single node, use selectSingleNode().

    Make sure you test for the node's existence before you use any properties.  For example, this will throw an exception:

    Set objNode = xmlDoc.selectSingleNode("/IBM/List_Wrapper/_bp/record_no")
    stringValue = objNode.text

    ...if the selectSingleNode() doesn't actually select a node.  In Javascript you can test for null, but in VBScript you test for nothing.

    Mike Sharp

    Author Comment

    I am getting a object doesn't support this property/method message.


    Here is the declaration:

    'Declaring variables

          Dim objFSO, objFolder, objFile, colFiles, strCpmsErrorDir, strCpmsSuccDir
          Dim objXMLDom, objNodes, objNodeItem, objCpmsRS, objCpmsCnn, objNode
          Dim strFileName, objMessages, objMsg, strPackageNo

    '  Declaring VB and variable constants

          CONST adOpenKeySet = 1
          CONST adLockOptimistic = 3
          CONST adCmdStoredProc = &H0004
          CONST adParamInput = &H0001
          CONST adVarChar = 200
          CONST adInteger = 3
          CONST adRunAsync = &H00000010
          CONST adParamReturnValue = &H0004

          strCpmsErrorDir = DTSGlobalVariables("cpmsErrorFilePathRoot").Value & MonthName(Month(Date()), true) & "-" & DatePart("d", Date())  & "-" & Year(Date())
          strCpmsSuccDir = DTSGlobalVariables("cpmsSuccFilePathRoot").Value & MonthName(Month(Date()), true) & "-" & DatePart("d", Date())  & "-" & Year(Date())
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          Set objFolder = objFSO.GetFolder(strCpmsErrorDir)
          Set colFiles = objFolder.Files

          Set objXMLDOM = CreateObject("MSXML2.DOMDocument.4.0")
          Set objXMLDOC = CreateObject("Microsoft.XMLDOM")
          objXMLDOM.validateOnParse = False      


    Here is the code to parse the value.  What am I doing wrong?

    ' Checking for files with an extension of XML

                            If UCase(Right(objFile, 4)) = ".XML" Then

    ' Parsing the file name to get the package number
                            strFilename = strCpmsErrorDir & "\" & objFile.Name
                            strPackageNo = Mid(objFile.Name, 1, len(objFile.Name)-4)

                            objNode = objXMLDoc.selectSingleNode("/IBM/List_Wrapper/_bp")
                            msgbox objNode.text

    LVL 26

    Accepted Solution

    Don't mix the MSXML object models.  And, there's no reason whatsoever to use Microsoft.XMLDOM.  That dates back to 1997/1998, and is non-compliant.  Although, MSXML 3 will actually handle calls to that object.  But I don't see why you need two XML Dom document objects anyway.

    I would do several things here.  First of all, you need some error handling code.  You need to ensure that the XM DomDocument object actually parsed the file before you use it, just as you need to check for the existence of a node before you use it.  The load() method will return a boolean if the load was successful.  Then you can check the parseError object to make sure there were no parsing errors.  Then you select nodes.  I'd also add a few more lines:

         Set objXMLDOC = CreateObject("MSXML2.DOMDocument.4.0")
         objXMLDOC.setProperty "SelectionLanguage", "XPath"
         objXMLDOC.validateOnParse = False    
         objXMLDOC.resolveExternals = False

         bIsLoaded = objXMLDOC.load(strFilename)

         If bIsLoaded Then

             if objXMLDOC.parseError.errorCode <> 0 then
                set myErr = objXMLDOC.parseError;
                strErrorMessage = "You have error " & myErr.reason
             end if

             ' don't forget to use Set, because you're assigning an object to a variable
             Set objNode = objXMLDOC.selectSingleNode("/IBM/List_Wrapper/_bp")
             If Not objNode Is Nothing Then
                  strValue = objNode.text
             End If
         End If

    I'm not much of a VBScript programmer, so there may be syntax typos in there...

    Mike Sharp

    Author Comment

    That worked.  Thank you very much...  It would have taken me a while to figure that one out...


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    733 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

    18 Experts available now in Live!

    Get 1:1 Help Now