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("/IB M/List_Wra pper/_bp") . Using VBSCript and DOM. Tried it several times with no luck.
- <IBM>
- <error>
<status_code>500</status_c ode>
<message>(407)Proxy Authentication Required</message>
</error>
<_shortname>IBM</_shortnam e>
<_bpname>Purchase Orders</_bpname>
<_projectnumber>1000</_pro jectnumber >
<_servicename>createRecord </_service name>
- <List_Wrapper>
- <_bp>
<record_no />
- <IBM>
- <error>
<status_code>500</status_c
<message>(407)Proxy Authentication Required</message>
</error>
<_shortname>IBM</_shortnam
<_bpname>Purchase Orders</_bpname>
<_projectnumber>1000</_pro
<_servicename>createRecord
- <List_Wrapper>
- <_bp>
<record_no />
ASKER
No. IBM is the root element
Like:
Set objNode = xmlDoc.selectSingleNode("/
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("/
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
ASKER
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("cpmsEr rorFilePat hRoot").Va lue & MonthName(Month(Date()), true) & "-" & DatePart("d", Date()) & "-" & Year(Date())
strCpmsSuccDir = DTSGlobalVariables("cpmsSu ccFilePath Root").Val ue & MonthName(Month(Date()), true) & "-" & DatePart("d", Date()) & "-" & Year(Date())
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objFolder = objFSO.GetFolder(strCpmsEr rorDir)
Set colFiles = objFolder.Files
Set objXMLDOM = CreateObject("MSXML2.DOMDo cument.4.0 ")
Set objXMLDOC = CreateObject("Microsoft.XM LDOM")
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/Lis t_Wrapper/ _bp")
msgbox objNode.text
==========================
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("cpmsEr
strCpmsSuccDir = DTSGlobalVariables("cpmsSu
Set objFSO = CreateObject("Scripting.Fi
Set objFolder = objFSO.GetFolder(strCpmsEr
Set colFiles = objFolder.Files
Set objXMLDOM = CreateObject("MSXML2.DOMDo
Set objXMLDOC = CreateObject("Microsoft.XM
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
msgbox objNode.text
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked. Thank you very much... It would have taken me a while to figure that one out...
Thanks
Thanks
by any chance an xmlns attribute higher up in the file?
Cheers!