Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / May 2007



Tip: Looking for answers? Try searching our database.

Retrieving XML Attributes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew - 18 May 2007 01:32 GMT
Hi All,

I have an XML project that I need some help with, what I need to achieve is
to get a group of elements from an XML file, then iterate through each
element to select only certain attributes. See below for example:

-- Example XML File --

<ROWDATA>
<ROW ATTRIBUTE1="AAA" ATTRIBUTE2="BBB" ATTRIBUTE3="CCC" ATTRIBUTE4="DDD"/>
<ROW ATTRIBUTE1="111" ATTRIBUTE2="222" ATTRIBUTE3="333" ATTRIBUTE4="444"/>
</ROWDATA>

-- End XML File --

1. I need to build a nodelist of all elements called ROW (of which there are
two)
2. Start a loop for each element
3. Then I need to select ONLY attribute1 and attribute4 from the element
4. Store each attribute (of which there are two) into variables and process
them
5. Continue loop to next element

The only way I have been able to achieve this so far it to get a nodelist of
all the ROW elements, then iterate through each attribute and if the
attribute matches the one i am after, store it to a variable. The problem
with this approach is that is is VERY time consuming to loop through in this
manner when you have many attributes per element.

Any help would be great,

Please reply via Newsgroup!

Thanks,
Andrew
Tony Proctor - 24 May 2007 21:23 GMT
Here's a bit of code that just picks out the rows that have an ATTRIBUTE3
Andrew:

Private Sub Form_Load()
Dim oDOM As MSXML2.DOMDocument
Dim oNodeList As MSXML2.IXMLDOMNodeList
Dim oNode As MSXML2.IXMLDOMNode

Const XMLSTR As String = "<ROWDATA>" & _
"<ROW ATTRIBUTE1=""AAA"" ATTRIBUTE2=""BBB"" ATTRIBUTE3=""CCC""
ATTRIBUTE4=""DDD""/>" & _
"<ROW ATTRIBUTE1=""111"" ATTRIBUTE2=""222"" ATTRIBUTE3=""333""
ATTRIBUTE4=""444""/>" & _
"</ROWDATA>"

   Set oDOM = New MSXML2.DOMDocument
   With oDOM
       .async = False
       .loadXML XMLSTR
       If .parseError.errorCode <> 0 Then
           Debug.Print .parseError.reason
       End If
       For Each oNode In .selectNodes("/ROWDATA/ROW[@ATTRIBUTE3]")
           Debug.Print oNode.Attributes.getNamedItem("ATTRIBUTE3").Text
       Next oNode
   End With
End Sub

This can easily be generalised to pick all the rows having an arbitrary
attribute, and made to read from a read file rather than a test string

   Tony Proctor

> Hi All,
>
[quoted text clipped - 31 lines]
> Thanks,
> Andrew
Andrew - 31 May 2007 05:21 GMT
Thanks Tony!!

> Here's a bit of code that just picks out the rows that have an ATTRIBUTE3
> Andrew:
[quoted text clipped - 71 lines]
>> Thanks,
>> Andrew
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.