Extracting A Tag Element Value From An XML Document

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>
  <status_code>500</status_code>
  <message>(407)Proxy Authentication Required</message>
  </error>
  <_shortname>IBM</_shortname>
  <_bpname>Purchase Orders</_bpname>
  <_projectnumber>1000</_projectnumber>
  <_servicename>createRecord</_servicename>
- <List_Wrapper>
- <_bp>
   <record_no />
RVattakunntelAsked:
Who is Participating?
 
rdcproCommented:
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.async=False
     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...

Regards,
Mike Sharp
0
 
Geert BormansInformation ArchitectCommented:
Hi RVattakunntel,

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

Cheers!
0
 
RVattakunntelAuthor Commented:
No. IBM is the root element
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
rdcproCommented:

Like:

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.


Regards,
Mike Sharp
0
 
RVattakunntelAuthor Commented:
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")
      objXMLDOC.async=False
      objXMLDOM.async=False
      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)

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




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

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.