INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need help getting started

Need help getting started

(OP)
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

RE: Need help getting started

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.load(strXML) Then  'strXML is the string with XML'
  '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 IXMLDOMNode
Set VendorApp = objXML.ChildNodes
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

"We had to turn off that service to comply with the CDA Bill."
- The Bastard Operator From Hell

RE: Need help getting started

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
        If Not .Load(strXML) Then
           MsgBox "Error: " objXML.parseError.reason & vbNewLine & "line: " & XML.parseError.Line & vbNewLine & "col: " & XML.parseError.linepos

        End If
End With
winky smile

Cheers,
MakeItSo

"We had to turn off that service to comply with the CDA Bill."
- The Bastard Operator From Hell

RE: Need help getting started

(OP)
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.

RE: Need help getting started

Hi batteam

Quote:

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.

"We had to turn off that service to comply with the CDA Bill."
- The Bastard Operator From Hell

RE: Need help getting started

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close