sXMLfilepath =”C:\ReadXML.xml”
strQuery = “/ProfilesInfo/Profiles/ProfileInfo/Profile/Fields/Field [Name=’ProfileUri’ or Name= ‘Age’] /(Name|Value)”
call ReadXMLValueFromStringQuery( sXMLfilepath ,strQuery)
Public Function ReadXMLValueFromStringQuery(byval sXMLfilepath , byval strQuery)
Set xmlDoc = CreateObject( “Microsoft.XMLDOM” )
xmlDoc.Async = “False”
xmlDoc.Load( sXMLfilepath )
If xmlDoc.parseError.errorCode<>0 Then
Print “Invalid XMLFile/No file ”
else
‘strQuery = “/ProfilesInfo/Profiles/ProfileInfo/Profile/Fields/Field [Name=’ProfileUri’ or Name= ‘Age’] /(Name|Value)”
Set colNodes = xmlDoc.selectNodes( strQuery )
If colNodes.length>0 Then
For Each objNode in colNodes
Print objNode.nodeName & “: ” & objNode.text
Next
else
Print “Nothing Found for this strQuery =” & strQuery
End If
End If
Set xmlDoc = nothing
Set colNodes = nothing
End Function
Set xmlDoc =nothing
Set ElemList =nothing
Set xmlDoc = CreateObject(“Msxml2.DOMDocument”)
xmlDoc.load(“C:\TestData.xml”)
Set ElemList1= xmlDoc.DocumentElement.ChildNodes
For i = 0 to ElemList1.length-1
msgbox ElemList1.item(i).text
next
Set ElemList = xmlDoc.getElementsByTagName(“book”)
filepath = ElemList.item(0).getAttribute(“author”)
MsgBox filepath
Set ElemList = xmlDoc.getElementsByTagName(“title”)
plot = ElemList.item(0).Text
MsgBox plot
Let’s get started.
In your editor, IDE or whatever, type the following code:
Set xmlDoc = CreateObject( "Microsoft.XMLDOM" )
xmlDoc.Async = "False"
xmlDoc.Load( "dvdcoll.xml" )
If you have an integrated object browser, you’ll probably see a reference to MSXML being inserted.
We’ll start with simple query, display each DVD’s title:
strQuery = "/Collection/DVD/Title"
Set colNodes = xmlDoc.selectNodes( strQuery )
For Each objNode in colNodes
WScript.Echo objNode.nodeName & ": " & objNode.text
Next
The result will look like this:
Title: Wild Down Under
Title: Pride
Title: Gandhi
Title: The Abyss
Title: Shouf Shouf Habibi
So, why was the query built like this?
Take a look at the image on the right, a fragment of a screenshot of XML Notepad’s “XSL Output” window with our XML file loaded (or open the XML file in a plain text editor).
What we see is an opening tag <Collection>
as the first tag. This translates to /Collection
in the query.
Then, between the opening <Collection>
and closing </Collection>
tags (“inside” the Collection
), note the “recordsets” of <DVD>
and </DVD>
tags. These translate to the DVD
part of the query: in the root we find Collections
, and inside (or under) Collections
we find DVD
— just like a directory structure on a disk.
Inside (or under) DVD
there are several tags, Title
being one of them. So now we have /Collection/DVD/Title
.
Note: |
Queries are case sensitive! Capitalization must match the XML tags exactly. |
Now let’s move to the next level of complexity for our queries, display multiple properties:
strQuery = "/Collection/DVD/ ( Title | Genres )"
Note: |
This is the query definition only. Replace the previous query definition by this new one. The rest of the script remains unaltered. |
The part of the query between parentheses is a list of choices, separated by “pipe” characters (|
).
Tags will be listed if they match any of these choices, so Title
as well as Genres
will be shown for each DVD in the collection.
The result will look like this:
Title: Wild Down Under
Genres: Documentary Special Interest
Title: Pride
Genres: Documentary
Title: Gandhi
Genres: Drama Classic
Title: The Abyss
Genres: Science-Fiction Suspense/Thriller
Title: Shouf Shouf Habibi
Genres: Comedy
The genres are listed as single lines, where each line can contain one (or zero) or more genres.
If you look further down in the XML structure, you’ll notice that some keys or tags have subkeys: Genres
for example uses Genre
subkeys.
Subkeys are used when multiple values are possible. They can be viewed best in XML Notepad’s treeview.
To list each individual genre we must use /Collection/DVD/Genres/Genre
.
So next, let’s display the Title and each individual Genre:
strQuery = "/Collection/DVD/ ( Title | Genres/Genre )"
Roughly translated: display anything that matches "/Collection/DVD/Title"
or "/Collection/DVD/Genres/Genre"
The result will look like this:
Title: Wild Down Under
Genre: Documentary
Genre: Special Interest
Title: Pride
Genre: Documentary
Title: Gandhi
Genre: Drama
Genre: Classic
Title: The Abyss
Genre: Science-Fiction
Genre: Suspense/Thriller
Title: Shouf Shouf Habibi
Genre: Comedy
Notice how some DVDs have multiple Genres.
OK, listing properties for each item in a collection no longer has any secrets for you.
Let’s select specific items (DVDs) from the collection.
List the titles and genres of all documentaries:
strQuery = "/Collection/DVD [ Genres/Genre = 'Documentary' ] / ( Title | Genres/Genre )"
The result will look like this:
Title: Wild Down Under
Genre: Documentary
Genre: Special Interest
Title: Pride
Genre: Documentary
Title: March of the Penguins
Genre: Documentary
Title: Alaska: Spirit of the Wild
Genre: Documentary
Genre: Special Interest
Title: Wilderness Journey - Canyon Suites
Genre: Documentary
Genre: Special Interest
Genre: Music
It is possible to select based on genre without displaying it:
strQuery = "/Collection/DVD [ Genres/Genre = 'Documentary' ] /Title"
would only display the titles of the documentaries.
Finally, the selection can be narrowed down by using and
and or
. Note that these are case sensitive, so And
will not work.
List titles and all formats for each documentary that supports 16×9 format:
strQuery = "/Collection/DVD " _
& "[ Genres/Genre = 'Documentary' and Format/Format16X9 = 'True' ] " _
& "/ ( Title | Format/* )"
The result will look like this:
Title: Wild Down Under
FormatAspectRatio: 1.78
FormatVideoStandard: PAL
FormatLetterBox: True
FormatPanAndScan: False
FormatFullFrame: False
Format16X9: True
FormatDualSided: False
FormatDualLayered: False
FormatFlipper: False
Title: Pride
FormatVideoStandard: PAL
FormatLetterBox: True
FormatPanAndScan: False
FormatFullFrame: True
Format16X9: True
FormatDualSided: False
FormatDualLayered: True
FormatFlipper: False