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.

Creating User-Defined Types Using XML Document Object Model

 Keywords:  User Defined Types Using XML Document Object Model

I'm not so great with multi-dimensional arrays so while viewing the XML Parser Data I came up with the idea of creating data types which are flexible and great in languages like VB/VBA. Stan's great posts on "Roll Your Own" got me thinking, but it seems VBScript can't handle user-defined data-types either.

XML can handle just about everything. So here's my first shot at creating user-defined types for Winbatch, using the XML Document Object Model as an intermediary. The caveat being you must pass the object/field references as a string, and it is case-sensitive, but you can have spaces within the Field Names.

I'm using Winbatch 2002G, XML 4.0 on Windows XP Home.

cheers Jay Alverson

#definesubroutine CreateDataType(number, name, fieldlist)
   ;   open an XML document...
   xmlDoc = objectopen("Msxml2.DOMDocument.4.0")
   xmlDoc.async = @false
   RootElement = xmlDoc.createElement("CustomDataType")
   xmlDoc.appendChild(RootElement)
   ;   create the main element...
   DataTypeElement = xmlDoc.createElement("DataTypeName")
   ;   give it an ID attribute...
   DataTypeAttribute = xmlDoc.createAttribute("id")
   DataTypeAttributeText = xmlDoc.createTextNode(name)
   DataTypeAttribute.appendChild(DataTypeAttributeText)
   ;   give it a record number...
   DataTypeAttribute2 = xmlDoc.createAttribute("rnum")
   DataTypeAttributeText2 = xmlDoc.createTextNode("1")
   DataTypeAttribute2.appendChild(DataTypeAttributeText2)
   ;   append the ID attribute...
   DataTypeElement.setAttributeNode(DataTypeAttribute)
   RootElement.appendChild(DataTypeElement)
   ;   append the record number attribute...
   DataTypeElement.setAttributeNode(DataTypeAttribute2)
   RootElement.appendChild(DataTypeElement)
   ;   build the field sub-elements...
   fieldlist = strreplace(fieldlist, ",", @tab)
   for ff = 1 to itemcount(fieldlist, @tab)   
   ;   now add fields for the Data Type...
      thisfield = itemextract(ff, fieldlist, @tab)
      FieldElement = xmlDoc.createElement("DataTypeField")
      FieldAttribute = xmlDoc.createAttribute("fldid")
      FieldAttributeName = xmlDoc.createTextNode(thisfield)
      FieldAttribute.appendChild(FieldAttributeName)
      ;   make each blank/null to start...
      FieldAttributeText = xmlDoc.createTextNode("")
      FieldElement.setAttributeNode(FieldAttribute)
      FieldElement.appendChild(FieldAttributeText)
      DataTypeElement.appendChild(FieldElement)
   next
   ;   create a clone of the main node...
   dNodes = xmlDoc.selectNodes("CustomDataType/DataTypeName")
   dataNode = dNodes.item(0)
   for ff = 1 to number-1
      newNode  = dataNode.cloneNode(@true)
      newAtts  = newNode.attributes
      rec = newAtts.getNamedItem("rnum")
      ;   give it a unique record number...
      rec.text = rec.text + ff
      RootElement.appendChild(newNode)
   next
#endsubroutine

#definesubroutine PutData(index, ObjectField, value)
   ; extract the ID and fieldnames...
   object = itemextract(1, ObjectField, ".")
   fieldname = itemextract(2, ObjectField, ".")
   ;   find the node with the matching record number...
   dNode = xmlDoc.selectSingleNode(strcat("CustomDataType/DataTypeName[@id = '", object, "' and @rnum = ", index, "]"))
   dChilds = dNode.childnodes
   ;   loop thru the child nodes...
   for cn = 0 to dChilds.length-1
      thisChild = dChilds.item(cn)
      tAtts = thisChild.Attributes
      thisfield = tAtts.getNamedItem("fldid")
      ;   if the fields match assign the value to it...
      if thisfield.text == fieldname then thisChild.text = value
   next
#endsubroutine

#definesubroutine GetData(index, ObjectField)
   ;   extract the ID and fieldnames...
   object = itemextract(1, ObjectField, ".")
   fieldname = itemextract(2, ObjectField, ".")
   ;   retrieve the node with the matching record number...
   dNode = xmlDoc.selectSingleNode(strcat("CustomDataType/DataTypeName[@id = '", object, "' and @rnum = ", index, "]"))
   dChilds = dNode.childnodes
   ;   loop thru the childnodes...
   for cn = 0 to dChilds.length-1
      thisChild = dChilds.item(cn)
      tAtts = thisChild.Attributes
      thisfield = tAtts.getNamedItem("fldid")
      ;   if a match is found return the value...
      if thisfield.text == fieldname
         return(thisChild.text)
      endif
   next
#endsubroutine

#definesubroutine FormatAndClipXML()
   ; the xml is a single flat line, so this makes it readable...
   theXML = xmlDoc.xml
   theXML = strreplace(theXML, "<DataTypeName", strcat(@crlf, "   <DataTypeName"))
   theXML = strreplace(theXML, "<DataTypeField", strcat(@crlf, "      <DataTypeField"))
   theXML = strreplace(theXML, "</DataTypeName>", strcat(@crlf, "   </DataTypeName>"))
   theXML = strreplace(theXML, "</CustomDataType>", strcat(@crlf, "</CustomDataType>"))
   clipput(theXML)
#endsubroutine

first = "Jay;Tom;Marty"
last  = "Alverson;Ingalls;Smith"
phone = "555-123-4567;555-304-1239;555-123-8904"

NumberOfRecords = itemcount(first, ";")

CreateDataType(NumberOfRecords, "Contact", "First Name,Last Name,Work Phone")

for z = 1 to itemcount(first, ";")
   firstname = itemextract(z, first, ";")
   lastname  = itemextract(z, last,  ";")
   wphone    = itemextract(z, phone, ";")

   PutData(z, "Contact.First Name", firstname)
   PutData(z, "Contact.Last Name",  lastname)
   PutData(z, "Contact.Work Phone", wphone)
next

for z = 1 to itemcount(first, ";")
   outline = strcat(GetData(z, "Contact.First Name"), @crlf, GetData(z, "Contact.Last Name"), @crlf, GetData(z, "Contact.Work Phone"))
   message("Record #%z%", outline)
next

FormatAndClipXML()

return
exit


Data Sample:

<CustomDataType>
   <DataTypeName id="Contact" rnum="1">
      <DataTypeField fldid="First Name">Jay</DataTypeField>
      <DataTypeField fldid="Last Name">Alverson</DataTypeField>
      <DataTypeField fldid="Work Phone">555-123-4567</DataTypeField>
   </DataTypeName>
   <DataTypeName id="Contact" rnum="2">
      <DataTypeField fldid="First Name">Tom</DataTypeField>
      <DataTypeField fldid="Last Name">Ingalls</DataTypeField>
      <DataTypeField fldid="Work Phone">555-304-1239</DataTypeField>
   </DataTypeName>
   <DataTypeName id="Contact" rnum="3">
      <DataTypeField fldid="First Name">Marty</DataTypeField>
      <DataTypeField fldid="Last Name">Smith</DataTypeField>
      <DataTypeField fldid="Work Phone">555-123-8904</DataTypeField>
   </DataTypeName>
</CustomDataType>


Article ID:   W15667
File Created: 2003:05:13:11:29:24
Last Updated: 2003:05:13:11:29:24