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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA: Extracting Data from XML Node 1

Status
Not open for further replies.

BitNet33

Technical User
Feb 2, 2011
21
CA
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
 
There are all sorts of ways to do this - and the [blue]Attributes[/blue] 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.IXMLDOM[red]Selection[/red]
Dim str As String

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

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

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
 
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
 
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"?
 
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
 
I'm still trying to learn this stuff
Thus the URL I gave you ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top