WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with XML

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

XPATH and XML Parsing


;// Winbatch - Things you can do with XPATH
;//
;// uses cd.xml sample xml file
;// Kudus to Jay Alverson, for starting the ball rolling
;//
;// Stan Littlefield - May 25, 2003
;/////////////////////////////////////////////////////////

cXML = StrCat(dirget(),"cd.xml")
If ! FileExist(cXML) Then Exit

oDOM = ObjectOpen("Msxml2.DOMDocument.4.0")
oDOM.async = @false
oDOM.Load(cXML)

;// display elements with price > 11.00

oNode = oDOM.SelectNodes("/catalog/cd[price>11.00]")
For i = 0 To (oNode.length-1)
   elem = oNode.Item(i)
   Message( "CD Price > 11.00",elem.Text)
Next

;// display 1st cd node - NOTE: there is no first() function
;// also note, cd[1] is cd[0] under IE 5.0 - go figure???
oNode = oDOM.SelectNodes("/catalog/cd[1]")
For i = 0 To (oNode.length-1)
   elem = oNode.Item(i)
   Message( "First CD",elem.Text)
Next

;// display last cd node
oNode = oDOM.SelectNodes("/catalog/cd[last()]")
For i = 0 To (oNode.length-1)
   elem = oNode.Item(i)
   Message( "Last CD",elem.Text)
Next

;// display Only Those from the UK
oNode = oDOM.SelectNodes("//cd[@country='UK']")
For i = 0 To (oNode.length-1)
   elem = oNode.Item(i)
   Message( "CD's from the UK",elem.Text)
Next



;// total price of all CD's in file
;// there is a sum() function in XPATH
oNode = oDOM.SelectNodes("/catalog/cd/price")
n = 0.00
For i = 0 To (oNode.length-1)
   elem = oNode.Item(i)
   n = n+elem.Text
Next

message("Sum of CD Prices",n)



ObjectClose(oDom)
Exit


cd.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="USA">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <price>9.90</price> 
  </cd>
  <cd country="USA">
    <title>Born In The USA</title>
    <artist>Bruce Springsteen</artist>
    <price>11.50</price>
  </cd>
  <cd country="UK">
    <title>Hide Your Pride</title>
    <artist>U-2</artist>
    <price>10.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title>
    <artist>Kansas</artist>
    <price>8.75</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title>
    <artist>Eagles</artist>
    <price>13.75</price>
  </cd>
  <cd country="AUS">
    <title>Live</title>
    <artist>AC-DC</artist>
    <price>10.90</price>
  </cd>
</catalog> 

Article ID:   W16159
File Created: 2004:03:30:15:43:00
Last Updated: 2004:03:30:15:43:00