See UPS OnLine Tools Developer's Guide for tool specific Xpaths and options for formatting XML Requests.
A document can be created using the MSXML3 Dom object. After the document has been created the node can added by calling the addNode function. To build an XML Document the AddNode function takes the tag of the parent node and generates a child then appends the child node to the parent node using calls to the DOM object.
The following code example illustrates creating an XML document:
Public Function createXMLDocument(rootname As String, xml As MSXML2.DOMDocument30) As Boolean On Error GoTo ErrHand
Dim docXML As New MSXML2.DOMDocument30 Dim parentNode As IXMLDOMNode Dim root As IXMLDOMElement Dim Prop As MSXML2.IXMLDOMProcessingInstruction
docXML.async = False Set root = docXML.createElement(rootname) docXML.loadXML root.xml Set parentNode = root
Set Prop = docXML.createProcessingInstruction("xml", "version = '1.0'") docXML.loadXML (Prop.xml & root.xml)
Set xml = docXML createXMLDocument = True Exit Function
ErrHand: createXMLDocument = False End Function |
The Parent node and the newTagName are required for the AddNode subroutine. If the textString is passed then the node is added to the parentnode with textString as the node value. If the "EMPTY_NODE" tag is passed in the textString field then a container node (newTagName) is added to the parent node. If the textString field is empty and the attrName and attrValue the attribute is added the parentnode. Otherwise no node is added.
The following code example illustrates adding a node:
Public Function AddNode(doc As MSXML2.DOMDocument30, strParentTag As String, newTagName As String, _ textString As String, attrName As String, AttrValue As String) On Error GoTo ErrHand Dim Item As MSXML2.IXMLDOMElement Dim root As MSXML2.IXMLDOMElement Dim oAttr As MSXML2.IXMLDOMAttribute Dim parentTag As MSXML2.IXMLDOMNode Dim Node As MSXML2.IXMLDOMNode Dim pNode As MSXML2.IXMLDOMNode Dim child As MSXML2.IXMLDOMNode Dim ThisNode As MSXML2.IXMLDOMText AddNode = True ' we are getting a nodelist because it allows us to specify the parent element ' by its string name; that way, we don't have to pass Elements around Dim N1 As MSXML2.IXMLDOMNodeList Set parentTag = doc Set N1 = doc.getElementsByTagName(strParentTag) If N1.length > 0 Then Set Parent = N1.Item(N1.length - 1) Else Set Parent = parentTag End If If Not (StrComp(textString, "") = 0) Then
Set Node = doc.createNode(NODE_ELEMENT, newTagName, "") If Not (StrComp(textString, "EMPTY_NODE") = 0) Then Node.nodeTypedValue = textString End If Parent.appendChild Node
AddNode = True
Else If Not (((StrComp(attrName, "") = 0) Or (StrComp(AttrValue, "") = 0))) Then Set oAttr = doc.createAttribute(attrName) Parent.setAttribute attrName, AttrValue
AddNode = True Else AddNode = False End If End If Exit Function ErrHand: AddNode = False End Function |
Parsing XML with VB6. The DOM Object parses the document when it is loaded. If the document is not valid then the appropriate DOM Exception is returned. The "getElementsByTagName()" function can be used to access nodes within the Document as shown below.
The following code example illustrates parsing an XML document:
Public Function FillXML(xnodes() As String, strXML As String) As String 'This method requires that the array size be greater than or equal to the amount 'of "writable" nodes in the XML document. Also, the order of the values in the 'array should sync up with the order of their respective "writable" nodes in the 'XML document. On Error GoTo ErrFill Dim I As Integer Dim str As String Dim objXMl As DOMDocument30 Dim objXMLList As IXMLDOMNodeList Set objXMl = New DOMDocument30 objXMl.loadXML strXML Set objXMLList = objXMl.getElementsByTagName("*") t = 0 MaxNodes = objXMl.getElementsByTagName("*").length For I = 0 To MaxNodes - 1 'Here we make a list of child nodes for every element in the list. Set ElementList = objXMLList.Item(I).childNodes 'If there is only one child in the list then we write the value in the array to 'that node. If ElementList.length = 1 Then objXMl.getElementsByTagName("*").Item(I).Text = xnodes(t) t = t + 1 End If Next I FillXML = True strXML = objXMl.xml Exit Function ErrFill: FillXML = False End Function |
Copyright � 2006 United Parcel Service of America, Inc.