Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help getting started

Status
Not open for further replies.

batteam

Programmer
Sep 12, 2003
374
0
0
US
I have the following simple XML file located in a folder on my computer ("H:\UserAuthRequest.xml")

'<?xml version="1.0" encoding="utf-8" ?>
'- <UserAuthRequest>
' <UserLoginName>PRUSL</UserLoginName>
' -
'- <UserPswd>
' <CryptType>NONE</CryptType>
' <Pswd>N/A</Pswd>
' </UserPswd>
' -
'- <VendorApp>
' <VendorName VendorCode="0029">PRU</VendorName>
' <AppName>NB Pending Case Status Feed</AppName>
' <AppVer>Pru_IEXMLCaseStatus1.0</AppVer>
' </VendorApp>
' </UserAuthRequest>

What I need is just a few lines of code to get started to pull out, for example, the VendorName and AppName. I have Microsoft XML, v2.6 reference loaded and am using VBA in MSAccess 2007.

My current code snippet is:

strXML = "H:\UserAuthRequest.xml"
Dim objXML As MSXML2.DOMDocument
Set objXML = New MSXML2.DOMDocument

If Not objXML.loadXML(strXML) Then 'strXML is the string with XML'
'Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
End If

Dim VendorApp As IXMLDOMNode
Set VendorApp = objXML.ChildNodes
Debug.Print VendorApp.SelectSingleNode("VendorName").Text
Debug.Print VendorApp.SelectSingleNode("AppName").Text
Debug.Print VendorApp.SelectSingleNode("AppVer").Text
end sub

I get a debug error at the .firstChild line. - Wrong datatype.

I know the code needs some work. Any assistance to just get started on this would be greatly appreciatedl
 
Hi there,

you have two major errors here:
Code:
If Not objXML.loadXML(strXML) Then  'strXML is the string with XML'
  'Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
End If
loadXML expects XML content, not a file path.
You could use "loadXML("<?xml version=""1.0"" encoding=""UTF-8"" ?><root>...") or the like.
In order to load a file via file path into the objXML, you need the load method:
Code:
If Not objXML[b].load[/b](strXML) Then  'strXML [b][red]is the string with XML[/red][/b]'
  'Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
End If
Nope, it isn't the string with xml, it is the file path:
strXML = "H:\UserAuthRequest.xml"

2.)
Code:
Dim VendorApp As [b]IXMLDOMNode[/b]
Set VendorApp = objXML.[b]ChildNodes[/b]
VendorApp is a node whereas ChildNodes gives a NodeList, i.e. a collection of nodes.
Try this:
Code:
For Each VendorApp in obj.XML.documentElement.childNodes

Try this and you'll soon be set from there..

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
One more thing concerning your error report if the XML can't be loaded.
This is how I usually do it:
Code:
With objXML
        .setProperty "ProhibitDTD", False
        .async = False
        .resolveExternals = False
        .validateOnParse = False
        .preserveWhiteSpace = True
        [b]If Not .Load(strXML) Then 
           MsgBox "Error: " objXML.parseError.reason & vbNewLine & "line: " & XML.parseError.Line & vbNewLine & "col: " & XML.parseError.linepos[/b]
        End If
End With
;-)

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
MakeItSo: thanks for your response. Could you clarify a little on how to load the XML using a file path name and not a string, as I've done in my example? Your first two examples in the "code" boxes are identical and a little confusing as to how they relate to loading the XML. Thanks again for any help you can provide.
 
Hi batteam
MakeItSo: thanks for your response. Could you clarify a little on how to load the XML using a file path name and not a string, as I've done in my example?
The first code box is your code.
You seem to misunderstand here: You are NOT using a string. You are using a string variable but the value of your string variable is a file path!
That is why I slightly corrected your code in the second code box where I replaced your "loadXML" with "load".

The code in my second response does the same as your "If Not objXML.load"... part, just a little more error-avoiding.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
This stuff not making much sense to me. There are so many objects to Dim and so many properties and all the different combinations - not making much progress. For example, I attempt to run this code:

Dim xmlDoc As New MSXML2.DOMDocument30
Dim nodeBook As IXMLDOMElement
Dim nodeId As IXMLDOMAttribute
Dim sIdValue As String

xmlDoc.async = False
xmlDoc.Load ("H:\Books.xml")
If (xmlDoc.parseError.ErrorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox ("You have error " & myErr.reason)
Else: Set nodeBook = xmlDoc.SelectSingleNode("//book")
Set nodeId = nodeBook.getAttributeNode("id")
sIdValue = nodeId.XML
MsgBox sIdValue
End If
Exit Sub

and get the Object Variable Not Set debug error at the sIDValue = nodeId.XML line of code. Can you tell me why and how to resolve this? Thanks again for any assistance you can provide.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top