Section 10.9.  Inserting XML nodes

Prev don't be afraid of buying books Next

10.9. Inserting XML nodes

We have seen how to select nodes from a document using the selectSingleNode and selectNodes methods along with XPath expressions. We have also seen how to update a node's value, by assigning a value to the node's nodeTypedValue property.

There may be cases where you also want to insert or delete nodes from the hierarchy. In most cases, the standard InfoPath user interface will take care of this for you, in that it creates the basic structure of the document by default. The user can use the standard InfoPath functionality for inserting and deleting repeating or optional elements.

However, you may want to customize this functionality. Suppose we want a facility that allows users to repeat a line item in the order form, so that they can more easily enter similar items. We have accomplished this in our order form example by placing a Repeat button within the item row and associating it with the script shown in Example 10-9.

Example 10-9. Repeating an item
 function RepeatButton::OnClick(eventObj) {   //assign the current item node to a variable   var currentItem = eventObj.Source;   //copy the current item node   var copyOfItem = currentItem.cloneNode(true);   //insert the copy before the current item node   currentItem.parentNode.insertBefore(copyOfItem,currentItem); } 

Three simple lines of code are all that is needed to insert a copy of an item. On line 3, the current item (the one whose row contains the Repeat button that was clicked) is assigned to the variable currentItem. In this case, we use eventObj.Source to access this node.

When using the OnClick event, there is no eventObj.Site property as there was in the validation and calculation examples. In its place, the Source property retrieves the node that directly contains the button. In this case, it is the item element because the button appears within an item's row.

Line 5 assigns a copy of the current item node to the variable copyOfItem. Finally, line 7 inserts the copy directly before the current item row. The parentNode property of the current item is used to access the item's parent, namely the items node. The insertBefore method is then used to insert the current item as a child of items.

Amazon


XML in Office 2003. Information Sharing with Desktop XML
XML in Office 2003: Information Sharing with Desktop XML
ISBN: 013142193X
EAN: 2147483647
Year: 2003
Pages: 176

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