Using the xmlDocumentComponent

These steps show how to load an XML document and retrieve information from an example document using the xmlDocumentComponent and xmlElementComponent schema properties. The steps are organized into the following tasks.

The sample document is free-form and contains the following XML.

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book>

  <title lang="eng">Harry Potter And The Deathly Hallows</title>

  <pub_name>Arthur A. Levine Books</pub_name>

  <ISBN>0545139708</ISBN>

  <pages>784</pages>

  <price>10.19</price>

</book>

<book>

  <title lang="eng">The Sword in the Stone</title>

  <pub_name>Laurel Leaf </pub_name>

  <ISBN>0440984459</ISBN>

  <pages>256</pages>

  <price>16.49</price>

</book>

<book>

  <title lang="eng">The Story of King Arthur and His Knights</title>

  <pub_name>Sterling</pub_name>

  <ISBN>1402725035</ISBN>

  <pages>320</pages>

  <price>9.95</price>

</book>

<book>

  <title lang="eng">The Extraordinary Adventures of Alfred Kropp</title>

  <pub_name>Bloomsbury USA Children's Books</pub_name>

  <ISBN>1599900440</ISBN>

  <pages>352</pages>

  <price>8.99</price>

</book>

<book>

  <title lang="eng">Black Horses for the King</title>

  <pub_name>Del Rey</pub_name>

  <ISBN>0345408810</ISBN>

  <pages>217</pages>

  <price>6.95</price>

</book>

</bookstore>

 

Loading the OuterXML of an XML document

Follow these steps:

  1. Create a solution and assign this name to it: XMLDocument 1.

  2. Add a Windows form to the solution. This form is used to specify the name of the XML document to load and display information from the document. Complete the Windows form as shown here, then save the solution.

  3. Add an automation to the solution and assign this name to it: LoadDocument.

  4. The first part of the automation loads an XML document and displays the OuterXML in the txtXml text box. Begin by adding the xmlDocumentComponent xmlDocumentComponent from the Advanced tab on the toolbox to the Global tab in the component tray for the automation. By adding the component to the Global tray, other automations will have access to the document.


  5. By leaving the xmlDocumentComponent Schema property blank in the property window, the component defaults to FreeForm. For more information on the properties or methods used in these steps, see the xmlDocumentComponent topic. Add the xmlDocumentComponent Load (1 parameter string filename) to the automation. Complete this part of the automation as follows:

  6. Save and run the solution. Enter the full path and file name of the XML document to load and click the Load button. The resulting Windows form should look similar to this example.


     

Retrieving node information

The next part of the solution adds xmlDocumentComponent methods to retrieve the following information for a selected node or nodes in the document.

First, select the node or nodes are selected and then you use xmlElementComponent to get the element information. Follow these steps.

  1. Return to the LoadDocument automation, add the xmlDocumentComponent SelectSingleNode method and connect the design blocks as follows.

  2. Right-click the Result of the xmlDocumentComponent and select Extract Proxy. An xmlElementComponentProxy1 is added to the automation in the Local component tray and is displayed on the automation.

  3. The xmlElementComponentProxy1 lets you access properties of the selected node. Add the xmlElementComponentProxy1 HasChildNodes property and the WinForm Checkbox Checked property to the automation and connect the design blocks as shown below.

  4. To display the InnerText of the selected node, use the xmlElementComponentProxy1 InnerText property and connect this to the txtInnerText text box.

  5. To display the InnerXml of the selected node, use the xmlElementComponentProxy1 InnerXML property and connect this to the txtInnerXML text box.

  6. To display the LocalName of the selected node, use the xmlElementComponentProxy1 LocalName property and connect this to the txtLocalName text box.

  7. To display the Name of the selected node, use the xmlElementComponentProxy1 NodeName property and connect this to the txtName text box.

  8. To display the OuterXML of the selected node, use the xmlElementComponentProxy1 OuterXML property and connect this to the txtOuterXml text box.

  9. To display the Prefix of the selected node, use the xmlElementComponentProxy1 Prefix property and connect this to the txtPrefix text box.

  10. Save and run the solution. Enter the full path and file name of the XML document to load and click the Load button. Next, enter the XPath for the Book node: //book and click Select. Information for the first book node is retrieved. The resulting Windows form is shown here.


     

Retrieving child nodes

The next part of the automation uses the methods for the xmlElementComponent to retrieve information about the following nodes related to the selected node.

Follow these steps.

  1. Add the GetFirstChild method for the xmlElementComponentProxy1 to the automation. Right-click the element data output node and select the Extract Proxy option. The xmlElementComponentProxy2 is added to the Local tray and automation. This proxy contains the FirstChild node. Begin this part of the automation as follows.


    By connecting the True Result event node to the Proxy, only non-null First Child elements are used.

  2. Use the xmlElementComponentProxy2 ToString method to get FirstChild node information. Connect the Result of the ToString method to the txtFirstChild text box.

  3. Add the GetLastChild method for the xmlElementComponentProxy1 to the automation. Right-click the element data output node and select the Extract Proxy option. The xmlElementComponentProxy3 is added to the Local tray and automation. This proxy contains the LastChild node. Begin this part of the automation as follows.

  4. Use the xmlElementComponentProxy3 ToString method to get LastChild node information. Connect the Result of the ToString method to the txtLastChild text box.

  5. Save the solution. The completed automation is shown here.


     

  6. Run the solution. Enter the full path and file name of the XML document to load and click the Load button. Next, enter the XPath for the Book node: //book and click the Select button. Information for the first book node is retrieved. The resulting Windows form follows is shown here:



Part IV: Retrieving a list of child nodes

The last part of the automation uses a ListLoop component to process all of the child nodes under the selected node (xmlElementComponentProxy1) and display them in the list box.

  1. Add a ListLoop component to the automation. See the ListLoop Component topic for more information on this component.

  2. Add the xmlElementComponentProxy1 HasChildNodes property and ChildNodes property to the automation. Also add the Listbox Items property and Listbox.Items Clear method (access this method from the Explore Component Properties pane).

  3. Prepare the Listbox and start the ListLoop by connecting the design blocks as follows:


  4. To process the individual Child Nodes, you need to create a proxy from the Item output data node of the ListLoop. Right-click the Item output and select Extract Proxy. Connect the ItemYielded output event to the xmlElementComponentProxy4 input event as shown below.

  5. Use the ToString method for the xmlElementComponentProxy4 to get the text for the node and add this text to the Listbox using the Listbox.Items Add method (access this method from the Explore Component Properties pane). Connect the design blocks as shown below.

  6. Save the solution. The complete automation for this solution is shown here.

  7. Run the solution. Enter the full path and file name of the XML document to load and click the Load button. Next, enter the XPath for the Book node: //book and click the Select button. Information for the first book node is retrieved. The resulting Windows form is shown here.


     

Related information

xmlDocumentComponent

xmlElementComponent

Using the xmlDocumentComponent with Schema

 


Privacy | Trademarks | Terms of Use | Feedback

Updated: 18 June 2020

© 2016 - 2020 Pegasystems Inc.  Cambridge, MA All rights reserved.

 

OpenSpan data classification label