Writing to the DOM

[Previous] [Next]

In addition to accessing the values of certain properties in the DOM, you can also add nodes, prune nodes, and perform other manipulations while the document object is in memory. Let's say we want to create an XML document that has the following structure:

Listing 5-1. The desired structure for a particular XML document.

 

ToyCarPO.xml

<?xml version="1.0"?> <ToyCarPO_1.0 poNum = "18892" poDate="2000-02-21"> <header> <vendorNum>76671</vendorNum> <terms>1/10 net 30</terms> <shipTo>shipto</shipTo> <billTo>billto</billTo> </header> <body> <item needBefore="2000-02-29" needAfter="2000-02-22"> <partNum>SY120</partNum> <qty>10</qty> <price>12.43</price> </item> <item needBefore="2000-03-01" needAfter="2000-02-23"> <partNum>TQ331</partNum> <qty>100</qty> <price>0.443</price> </item> </body> </ToyCarPO_1.0>

We could create each element using a technique called concatenation in which we link together strings inside our program by building them up with the string-handling features of our programming language. This would require parsing the XML document ourselves. This would be a lot of work. We can save ourselves some effort by taking advantage of the functionality written into the Microsoft DOM object.

In this example, we will create an empty invoice document and use the DOM to add items under program control. Let's use Visual Basic to create the document. This is a fairly simple Visual Basic program. It runs when the form is loaded and opens message boxes that display the output. You can find this project on the companion CD in \Samples\Ch05\Struct.vbp.

First we need to instantiate a copy of the XML document object and load it with the root element. Table 5-7 describes the code in Listing 5-2 line by line.

Once we've instantiated and seeded the document, we can start to add elements and attributes. First let's add a couple of attributes to the root element. These attributes are detailed in Table 5-8.

Listing 5-2. Using loadXML to load a line of XML code.

 

buildXML.frm

1 Dim oXMLDoc As MSXML2.DOMDocument30 2 Dim rootElement 3 4 Set oXMLDoc = CreateObject("MSXML2.DOMDocument") 5 oXMLDoc.loadXML ("<ToyCarPO_1.0></ToyCarPO_1.0>") 6 set rootElement = oXMLDoc.documentElemen

Table 5-7. Creating the objects needed for our program.

Line Description
1-2 Declare the oXMLDoc and rootElementobjects.
4 Create an instance of the oXMLDoc document object.
5 Load the root element into the object, creating a single node that we can use as an anchor to add other nodes.
6 We could refer to the root element as oXMLDoc.documentElement for the rest of the program, but it is easier to create a new object as a reference point, so we instantiate rootElement.

Listing 5-3. Adding elements to a document object by using createAttribute.

 

buildXML.frm

1 Dim namedNodeMap 2 Set namedNodeMap = rootElement.Attributes 3 Dim newAtt 4 Set newAtt = oXMLDoc.createAttribute("poNum") 5 newAtt.Value = "19982" 6 namedNodeMap.setNamedItem newAtt

Table 5-8. Adding attributes to the root element.

Line Description
1-2 Create a node that will contain all the attributes. This namedNodeMap is an object that will contain all the attribute objects, each of which has a name and a value.
3-5 Create an attribute object, and give it a name. In this case, we are creating an attribute named poNum with a value of 19982. This is the name-value pair necessary to create an attribute.
6 The setNamedItem method adds a member to the named node map.

If we were to serialize (output to text) our XML document now, it would look like this:

We'll be creating a lot of attributes, so let's put the logic inside a reusable function:

 Public Function addAttribute(oDoc, nodeMap, attrName, attrValue) Dim newAtt Set newAtt = oDoc.createAttribute(attrName) newAtt.Value = attrValue nodeMap.setNamedItem newAtt End Function 

The addAttribute function is called from the application whenever we need to create an attribute with a name and value:

 addAttribute oXMLDoc, namedNodeMap, "poNum", "19982" addAttribute oXMLDoc, namedNodeMap, "poDate", Now() 

Our XML document now looks like this:

click to view at full size.

Now let's add some elements to our document. We create elements by using the createElement method, which creates an element node that stands by itself. We can then use the appendChild method to place that node into our document:

 Dim headerNode Set headerNode = oXMLDoc.createElement("header") rootElement.appendChild (headerNode) 

Now we have the following document:

click to view at full size.

Notice that once we create an element, we must fill it with content or it will be an empty element. We need to add some subelements to the header element and fill them with text content.

 Dim headerNode Set headerNode = oXMLDoc.createElement("header") Dim newChild Set newChild = oXMLDoc.createElement("vendorNum") newChild.Text = "76671" headerNode.appendChild (newChild) rootElement.appendChild (headerNode) 

The newChild object is created just like the headerNode element. It is an element node that is given a name (vendorNum) and a value (76671). The appendChild method adds the node to the headerNode object. This object is then grafted to the rootElement object with another appendChild method. The resulting XML looks like this:

click to view at full size.

We will be adding many elements with content, so let's place this code into a function:

 Public Function addElement(oDoc, node, elementName, elementValue) Dim newChild Set newChild = oDoc.createElement(elementName) newChild.Text = elementValue node.appendChild (newChild) End Function 

The function is called once for each element in the header element:

 addElement oXMLDoc, headerNode, "vendorNum", "76671" addElement oXMLDoc, headerNode, "terms", "1/10 net 30" addElement oXMLDoc, headerNode, "shipTo", "Shipto address" addElement oXMLDoc, headerNode, "billTo", "Billto address" 

We follow the same procedure to create the body element and its child elements. Table 5-9 walks you through the code line by line.

Listing 5-4. Building the body using the createElement method.

 

buildXML.frm

1 Dim bodyNode 2 Dim itemNode 3 Set bodyNode = oXMLDoc.createElement("body") 4 Set itemNode = oXMLDoc.createElement("item") 5 ' set attributes on item 6 Set namedNodeMap = itemNode.Attributes 7 addAttribute oXMLDoc, namedNodeMap, "needBefore", Now() + 8 8 addAttribute oXMLDoc, namedNodeMap, "needAfter", Now() + 14 9 ' set children of item 10 addElement oXMLDoc, itemNode, "partNum", "SY120" 11 addElement oXMLDoc, itemNode, "qty", "10" 12 addElement oXMLDoc, itemNode, "price", "12.43" 13 bodyNode.appendChild (itemNode) 14 15 Set itemNode = oXMLDoc.createElement("item") 16 ' set attributes on item 17 Set namedNodeMap = itemNode.Attributes 18 addAttribute oXMLDoc, namedNodeMap, "needBefore", Now() + 7 19 addAttribute oXMLDoc, namedNodeMap, "needAfter", Now() + 21 20 ' set children of item 21 addElement oXMLDoc, itemNode, "partNum", "TQ331" 22 addElement oXMLDoc, itemNode, "qty", "100" 23 addElement oXMLDoc, itemNode, "price", "0,443" 24 bodyNode.appendChild (itemNode) 25 rootElement.appendChild (bodyNode)

Table 5-9. A description of Listing 5-4, in which we create the body element and its child elements.

Line Description
1-2 The bodyNode and itemNode objects are declared. These objects will hold the element objects as we create them.
3-4 Instantiate copies of the nodes and use the createElement method to give them names.
7-8 Add attributes to the item element. Notice that we use the Attributes property to create namedNodeMap as part of the itemNode object. All attributes that we add by using the addAttribute function get attached to that node map.
10-12 The addElement function adds children to the itemNode element and gives them names and values.
13 Finally, we use the appendChild method to add the item node and its three child nodes (partNum, qty, and price) to the body node.
15-24 We use the same techniques described in this table to add another item.
25 We use the appendChild method to add the body node we built to the root element.

Now you can see that the document is complete:

click to view at full size.

Listing 5-5 shows the entire Visual Basic program:

Listing 5-5. An XML document built by using the DOM interfaces and Visual Basic.

 

buildXML.frm

Option Explicit Public Function addAttribute(oDoc, nodeMap, attrName, attrValue) Dim newAtt Set newAtt = oDoc.createAttribute(attrName) newAtt.Value = attrValue nodeMap.setNamedItem newAtt End Function Public Function addElement(oDoc, node, elementName, elementValue) Dim newChild Set newChild = oDoc.createElement(elementName) newChild.Text = elementValue node.appendChild (newChild) End Function Private Sub Form_Load() Dim oXMLDoc As MSXML2.DOMDocument30 Dim rootElement Set oXMLDoc = CreateObject("MSXML2.DOMDocument") oXMLDoc.loadXML ("<ToyCarPO_1.0></ToyCarPO_1.0>") Set rootElement = oXMLDoc.documentElement Dim namedNodeMap Set namedNodeMap = rootElement.Attributes addAttribute oXMLDoc, namedNodeMap, "poNum", "19982" addAttribute oXMLDoc, namedNodeMap, "poDate", Now() Dim headerNode Set headerNode = oXMLDoc.createElement("header") addElement oXMLDoc, headerNode, "vendorNum", "76671" addElement oXMLDoc, headerNode, "terms", "1/10 net 30" addElement oXMLDoc, headerNode, "shipTo", "Shipto address" addElement oXMLDoc, headerNode, "billTo", "Billto address" rootElement.appendChild (headerNode) MsgBox (rootElement.xml) Dim bodyNode Dim itemNode Set bodyNode = oXMLDoc.createElement("body") Set itemNode = oXMLDoc.createElement("item") ' set attributes on item Set namedNodeMap = itemNode.Attributes addAttribute oXMLDoc, namedNodeMap, "needBefore", Now() + 8 addAttribute oXMLDoc, namedNodeMap, "needAfter", Now() + 14 ' set children of item addElement oXMLDoc, itemNode, "partNum", "SY120" addElement oXMLDoc, itemNode, "qty", "10" addElement oXMLDoc, itemNode, "price", "12.43" bodyNode.appendChild (itemNode) Set itemNode = oXMLDoc.createElement("item") ' set attributes on item Set namedNodeMap = itemNode.Attributes addAttribute oXMLDoc, namedNodeMap, "needBefore", Now() + 7 addAttribute oXMLDoc, namedNodeMap, "needAfter", Now() + 21 ' set children of item addElement oXMLDoc, itemNode, "partNum", "TQ331" addElement oXMLDoc, itemNode, "qty", "100" addElement oXMLDoc, itemNode, "price", "0,443" bodyNode.appendChild (itemNode) rootElement.appendChild (bodyNode) MsgBox (rootElement.xml) End End Sub



XML and SOAP Programming for BizTalk Servers
XML and SOAP Programming for BizTalk(TM) Servers (DV-MPS Programming)
ISBN: 0735611262
EAN: 2147483647
Year: 2000
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net