Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

BitNet33 (TechnicalUser)
4 Mar 11 16:03
Hey,
So I'm opening XML files in Word and using VBA to extract data from certain XML nodes using DOM. My Code looks like this:

CODE

Sub Extract()
Dim xmlDoc As MSXML2.DOMDocument
Set xmlDoc = New MSXML2.DOMDocument
Dim nodeList As MSXML2.IXMLDOMNodeList
Dim str As String

xmlDoc.Load("C:Documents...\Document.xml")
Set nodeList = xmlDoc.getElementsByTagName("CUSTOMER")
str = nodeList.Item(0).XML
MsgBox str
End Sub
And this is what the XML node I'm using looks like:

CODE

<CUSTOMER
   NAME="Jim"
   LAST_NAME="Smith"
   ID="1784"
/>
With the code I have right now, all I can do is grab the entire "CUSTOMER" node, but all I want to only extract the "NAME" portion (Jim). My code prints out as:
"<CUSTOMER NAME="Jim" LAST_NAME="Smith" ID="1748"/>
Does anyone know of any way to access the "NAME" portion of the node without grabbing the whole thing? Or even what the "NAME", "LAST_NAME" and "ID" portions of the node are referred to as? Then maybe I could find a method that would access them. Any help would be awesome!

Thanks.
BitNet
Helpful Member!  TonyJollans (Programmer)
5 Mar 11 10:17
There are all sorts of ways to do this - and the Attributes collection of the nodeList is what holds the data you want. To simply do what you ask, quickly and dirtily, change the type of nodeList and specify what you want on the selection criteria:

CODE

Sub Extract()
Dim xmlDoc As MSXML2.DOMDocument
Set xmlDoc = New MSXML2.DOMDocument
Dim nodeList As MSXML2.IXMLDOMSelection
Dim str As String

xmlDoc.Load("C:Documents...\Document.xml")
Set nodeList = xmlDoc.getElementsByTagName("CUSTOMER/@NAME")
str = nodeList.Item(0).XML
MsgBox str
End Sub

This will give you NAME="Jim" - using the Text property (instead of the XML property) of the selection will just give you Jim.

PHV's link will give you full details of this and much more.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website

BitNet33 (TechnicalUser)
8 Mar 11 14:11
Awesome, thanks Tony. Works great! And thanks PHV for the link, I wasn't sure what I was looking for before but now I know they are "Attributes".

Thanks for the help guys.
BitNet
BitNet33 (TechnicalUser)
8 Mar 11 14:43
Oh, so another quick question.

Is there a way to grab just the "type" of the attribute?

For example, only grab "NAME"? Cause I know that .Text will just give me "Jim" and .XML will give me the whole thing, "NAME='Jim'". So how would I just get "NAME"?
TonyJollans (Programmer)
8 Mar 11 16:37
Yes ...

CODE

nodeList.Item(0).Name

... or, more clearly, perhaps ...

CODE

nodeList.Item(0).nodeName
 

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website

BitNet33 (TechnicalUser)
9 Mar 11 8:45
Awesome.

Sorry if those seemed like simple questions but I'm still trying to learn this stuff. I really appreciate the help.

Thanks again.
BitNet
PHV (MIS)
9 Mar 11 8:49
I'm still trying to learn this stuff
Thus the URL I gave you ...

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!

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