YOUR FEEDBACK
SOA Viewpoint: Have We Got It All Backwards with Software Assembly?
Bill Swanson wrote: Yes, Ford paid a "little bit better", just twice as mu...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
POWERBUILDER LINKS YOU MUST CLICK ON


Using .NET XML API in PowerBuilder
Exploring the .NET framework

Digg This!

XML is becoming the standard for data exchange. More and more software products and technologies are being built on top of it. Even the newest buzz word in Internet programming- AJAX - is related to XML. The good news is that .NET framework provides a very powerful API for manipulating XML, and you, as a PowerBuilder developer, can leverage on that API through the .NET interoperability feature released in PowerBuilder 11.

In this article, I will walk you through a code sample, showing how to:

  • Read and write XML files,
  • Traverse a XML DOM tree, and
  • Query XML data using XPath.
The code sample, which is a .NET Windows Forms target, contains three PBLs: usexml.pbl, netxml.pbl, and netui.pbl. The majority of the code is in usexml.pbl, while netxml,pbl and netui.pbl provide some utilities for XML and GUI effects, respectively. Figure 1 shows the main window of the sample.

Writing XML files
Clicking on the "Writing XML" button will open up the "Writing XML" window, as shown in Figure 2. The window contains a DataWindow and two buttons. When you click on the "Save as XML" button, a GetSaveFileName dialog will pop up prompting you to specify a file name and then save the DataWindow to the file in XML format (see Listing 1). The root node - CustomerDetails - contains a number of Customer nodes, which, in turn, contain FirstName, LastName, CompanyName, and PhoneNumber nodes.

Now let's examine the code. Listing 2 is the script of the clicked event of the "Save as XML" button. The script opens up a GetSaveFileName dialog. If a file name is specified correctly, it calls the saveXmlFile() function (Listing 3).

The function creates an instance of the .NET XmlWriter class and writes a CustomerDetails node (the root node). Then for each row of the DataWindow, it writes a Customer, then under that, writes four nodes for the FirstName, LastName, CompanyName, and PhoneNumber, respectively.

The XmlWriter class is an abstract class. The real type of the object doing the job is actually XmlTextWriter, which provides a fast, non-cached, forward-only way of generating streams or files containing XML data. The major methods and properties of the class include Close, Flush, Formatting, WriteAttribues, WriteAttributeString, WriteComment, WriteElementString, WriteElementString, WriteEndAttribute, WriteEndDocument, WriteState, and WriteStartDocument. For details, please refer to MSDN.

Reading XML files
The w_readxml window (Figure 3) demonstrates how to read an XML file and populate a TreeView with the elements of the XML file.

The clicked event of the "Read XML" button calls the readXmlFile() function, which uses the .NET XmlTextReader class to read the elements from an XML file. The code of the readXmlFile() function is shown in Listing 4.

The function creates an instance of the XmlTextReader class, which provides forward-only, read-only access to a stream of XML data. The XmlTextReader.NodeType property indicates the type of the current node, which can be element, attribute, text, CDATA, comment, and so on. An element node can have attributes. The XmlTextReader.Name and XmlTextReader.Value properties are for getting the name and value of the current node. Please refer to MSDN for details.

In order to insert the XML nodes into the TreeView, the function maintains an array of TreeViewItem handles and named handles, representing the current tree branch, which allows it to traverse back from the leaf node to the root node.

If the element has attributes, it adds an node for each attribute under the element node. If the element has attributes, it adds a node for each attribute node under the element node. If the current node is text, it inserts a text node under the current element node.

Traversing XML DOM
The code of the readXMLFile and the populate Tree functions are shown in Listing 5 and 6. If you want to load an XML file into memory and then manipulate the DOM tree, the XmlDocument class is more appropriate for that purpose. The w_dom window, which looks very similar to w_readxml, shows how to use this class.

The main code is in the readXmlFile function of this window object. The function creates an instance of the .NET XmlDocument class, creates an instance of the XmlTextReader class, and loads the XML file into memory through the XmlTextReader object. With the DOM tree we can populate the TreeView pretty easily by calling the recursive function, populateTree.

Notice that we pass an n_xmlElement object to the populateTree function rather than passing a .NET XmlElement object directly. This is because PowerBuilder 11 doesn't allow you to use a .NET type as a parameter of a function. To overcome this limitation, the n_xmlElement NVO is defined to wrap an instance of XmlElement.

The code of the readXMLFile and the populateTree functions are shown in Listing 5 and 6. For each child node of the given XmlElement, the function adds it to the TreeView. If a child node is an element, the populateTree is called recursively to add the child node to the TreeView.

With the XML DOM in hand, you can add new nodes, remove nodes, or modify nodes of the DOM tree. You can also save the DOM tree to a file.

Querying XML data using XPath
The XmlDocument class has certain limitations. First of all, the entire document needs to be loaded into memory. So the class is not suitable for huge XML files. The XPathDocument and XPathNavigator classes are intended to address this, as they allow you to process XML data without loading the entire DOM tree.

The w_xpath window (Figure 4) demonstrates how to use XPathDocument and XPathNavigator classes.

In this screen shot, we choose the XML file generated with the w_writexml window and then ask the program to list all LastNames with the XPath expression, CustomerDetails/Customer/LastName.

In fact, you can choose any XML file and use appropriate XPath expression to the process the data.

The code is in the clicked event of the Search button (Listing 7). We first create an instance of XpathDocument, then create an XPathNavigator instance by calling the XpathDocument.CreateNavigator method, then get an XpathNodeIterator with the specified XPath expression. With the XpathNodeIterator in hand, we can iterate through the items and add them to the ListBox.

Further Readings on .NET XML APIs
I hope I have shown you the power of the .NET XML API. The .NET XML API allows you to do many other things besides those that are shown in the sample. To explore how to use the API, I highly recommend the following articles in addition to the MSDN site.

1)  XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation
(http://msdn.microsoft.com/msdnmag/issues/01/01/xml/).

2)  The ".NET XML Best Practices" series:
- Part I: Choosing an XML API (http://support.softartisans.com/kbview.aspx?ID=673).
- Part II: Reading XML Documents (http://support.softartisans.com/kbview.aspx?ID=674)
- Part III: Writing XML Documents (http://support.softartisans.com/kbview.aspx?ID=675)

Some GUI Effects
You may have already noticed that the shape of the "Write XML" button on the main window is oval. This is achieved by calling the f_makeOval function object.

Trying to resize the four sub-windows, you will see the controls anchoring to the borders of the window quite nicely. It is the f_anchorControl function object that does the trick. Please figure out how these two functions work by your own by examining the downloadable sample code.

Conclusion
The .NET interoperability feature of PowerBuilder 11 makes the .NET framework widely open to you. Explore the. NET framework and you will find many things that you can leverage on. For example, the System.Collections, System.IO, System.NET, and System.Security.Cryptography namespaces. There are still some limitations in .NET interoperability in PowerBuilder 11 (e.g., the code for calling .NET classes has to be inside "if defined pbdotnet/#end if" code blocks, and .NET classes can't be used as parameters and return types of functions). We will gradually remove the limitations and enhance the .NET interoperability feature in future releases.

About Xue-song Wu
Xue-song Wu, a staff software engineer in Sybase Asia Development Center, Singapore, team leader of PowerBuilder Virtual Machine and Compiler team.

PBDJ LATEST STORIES . . .
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
The PB Future: More on Graphs in PowerBuilder 11.5
Last week I posted a screen shot of the new 3D Rendering capabilities being added to some of the 3D graphs in PowerBuilder 11.5. It was met with mixed reviews on the PowerBuilder Futures newsgroup (forums.sybase.com) so I went back to the drawing board to see what I could come up with.
BluePhoenix Expands Modernization Collaboration with Microsoft
BluePhoenix announced that it has expanded its collaboration with Microsoft on legacy modernization projects. The collaboration provides customers moving their applications or databases to .NET-based environments the best in both modernization services and technical support. BluePhoeni
Sybase PowerBuilder Delivers AJAX and .NET Enhancements Enabling Rich Internet Application Development
Sybase announced that AJAX development capabilities and further Microsoft .NET enhancements have been added to the latest version of Sybase PowerBuilder 11, the premier 4GL rapid application development (RAD) tool. PowerBuilder 11.2 represents another milestone in the PowerBuilder road
PowerBuilder 11.2 Released: Sybase's Flagship IDE
Sybase has released the production version of its flagship .NET development tool - PowerBuilder version 11.2. This latest release of its premier IDE for RAD includes not only standard fixes but also a good list of new features. Here is the 'Coles Notes' version of these new features.
PowerBuilder Takes You To .NET
In June of 2007, Sybase released PowerBuilder 11. PowerBuilder developers can now deploy PowerBuilder components as .NET Assemblies or as .NET Web Services. A PowerBuilder developer can now create these .NET resources so that those who develop .NET solutions can benefit from PowerBuild
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING POWERBUILDER / SYBASE NEWS
Sybase and Sun Set Guinness World Record for World's Largest Data Warehouse
Sybase, Inc. (NYSE:SY), the largest enterprise software and services company exclusively