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

MSXML

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

Parsing XML Using Msxml2.DOMDocument.4.0

 Keywords:  XML Parse Parsing MSXML Msxml2 DOMDocument

MENU.XML

A sample XML File.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<breakfast_menu>
  <food>
     <name>Belgian Waffles</name>
     <price>$5.95</price>
     <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
     <calories>650</calories>
  </food>
  <food>
     <name>Strawberry Belgian Waffles</name>
     <price>$7.95</price>
     <description>light Belgian waffles covered with strawberries and whipped cream</description>
     <calories>900</calories>
  </food>
  <food>
     <name>Berry-Berry Belgian Waffles</name>
     <price>$8.95</price>
     <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
     <calories>900</calories>
  </food>
  <food>
     <name>French Toast</name>
     <price>$4.50</price>
     <description>thick slices made from our homemade sourdough bread</description>
     <calories>600</calories>
  </food>
  <food>
     <name>Homestyle Breakfast</name>
     <price>$6.95</price>
     <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
     <calories>950</calories>
  </food>
</breakfast_menu>

XMLPARSE.WBT

A sample script that parses an XML File using Msxml2.DOMDocument.4.0.
xfile = DirScript() : "menu.xml"
xmlDoc = ObjectOpen("Msxml2.DOMDocument.4.0")
xmlDoc.async = @FALSE
xmlDoc.load(xfile)

err = xmlDoc.parseerror
If err.errorCode Then GoSub ShowParseErrors


Message("Section Count", xmlDoc.selectNodes("/breakfast_menu/food").length)

count = 0
ForEach Section In xmlDoc.selectNodes("/breakfast_menu/food")
   count = count + 1
   name   = Section.ChildNodes(0).ChildNodes(0).text    ; Belgian Waffles
   price   = Section.ChildNodes(1).ChildNodes(0).text   ; $5.95
   desc   = Section.ChildNodes(2).ChildNodes(0).text    ; two of our famous Belgian Waffles with plenty of real maple syrup
   cals   = Section.ChildNodes(3).ChildNodes(0).text   ; 650
   data = ""
   data = ItemInsert(name, -1, data, @LF)
   data = ItemInsert(price, -1, data, @LF)
   data = ItemInsert(desc, -1, data, @LF)
   data = ItemInsert(cals, -1, data, @LF)
   Message(StrCat("Section # ", count), StrCat(Section.xml, @LF, @LF, data))
   If count > 4 Then Break
Next

Section = 0
xmlDoc = 0

Exit

:ShowParseErrors
ec = err.errorCode
er = err.reason
el = err.line
elp = err.linepos
es  = err.srcText
msg = StrCat("Error Code: ", ec, @LF)
msg = StrCat(msg, "Reason: ", er, @LF)
msg = StrCat(msg, "Line: ", el, " ", "Column: ", elp, @LF)
msg = StrCat(msg, "Text: ", es)
Message("XML Parse Error", msg)

Article ID:   W18499
Filename:   Parse XML File Using MSXML.txt
File Created: 2013:08:09:07:23:20
Last Updated: 2013:08:09:07:23:20