Link to home
Start Free TrialLog in
Avatar of RVattakunntel
RVattakunntel

asked on

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 />
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Hi RVattakunntel,

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

Cheers!
Avatar of RVattakunntel
RVattakunntel

ASKER

No. IBM is the root element

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
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




ASKER CERTIFIED SOLUTION
Avatar of rdcpro
rdcpro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked.  Thank you very much...  It would have taken me a while to figure that one out...

Thanks