XML Part 3: Code Development

Web Languages
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Well, here we are at the third and final article in this series. In the first article, I talked about XML in general and explained the components that make up an XML document. I placed a heavy emphasis on the fact that XML is really a definition of tag semantics, not a definition of tags, like HTML. The tags you use and what you populate the tags with is up to you. As long as you follow the rigidity of XML, you'll be fine. The last sections of the first article dealt with reading, manipulating, validating, and transforming XML.

The second article discussed bringing XML to the next level, which is application-to-application (A2A), with Web Services being the enabler in this paradigm. While many XML standards provide security, message delivery, service advertisement, and subscription, there is no part of XML that actually does these things. And if you already have functionality such as encryption, why not just snap that into your XML infrastructure?

In this article, I'm going to get a little less scholastic and a little more hands-on. I'll show you a pseudo system and some of the code that could be used to support this system when it communicates with other systems. A good friend of mine, Steve Bos, who I have worked with for years, is an excellent programmer and has created the code in this article. He has several years of hands-on experience with XML, utilizing many of the technologies already discussed.

It's All in the Design

Let's start with a simple system. We are going to be reviewing a hotel reservation portal. There are three external entities: the customer, the hotel, and the credit card authorization firm. The portal provides a nice interface for what can be a batch and/or interactive system. Its all in the design!

The hotel may send pricing information to the portal either at a fixed interval (such as nightly or weekly) or when there are price changes. Availability information may also be sent in batch, or the portal can interactively query the hotel to see if there are rooms available when a user makes a request. While we will not code every possibility, it is important to look at different possibilities in the hopes that you come with some XML ideas on your own.

Tools Transform the Design

In the code examples that follow, Steve used Micosoft's C# (pronounced "C sharp") because of the vast array of utilities Microsoft has made available to quickly build XML/Web Service applications. Universal Description, Discovery, and Integration (UDDI) and Web Services Description Language (WSDL) definitions are created with the click of a button, which takes the complexity out of these services. Simple Object Access Protocol (SOAP) messages complement UDDI and WSDL by connecting to remote Web Services. I'll show you what a SOAP message should look like.

SOAP Message

Let's take a look at a SOAP message. (The text was formatted for readability. If this were real XML, you would not see any tabs or carriage returns/line feeds.) This SOAP message sends a request to the hotel on behalf of the user. It includes the hotel ID, client name, room options, and some payment information.


http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd="http://www.w3.org/2001/XMLSchema

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  
    
      2015432
      Mr. Wilson
      Honeymoon Suite
      2003-11-26
      2003-11-28
      Standard
      MC
      0000-0000000
      99/12
      Mr. Bud Wilson
    
  

Figure 1: This sample SOAP message requests hotel availability information.

Let's examine this code.

The standard first line indicates that we are using XML Version 1.0 and that the text encoding is UTF-8.

The next few lines set up some "run-time" properties of the document. A nice thing about XML is that you can follow the convention of anyone on the Internet in terms of naming your XML data elements. For our SOAP envelope, we will be using the standards from the Universal Resource Indicator (URI) http://www.w3.org/2001/XMLSchema-instance (name space xmlns). We will also be using the XML Schema Definition (xsd) from http://www.w3.org/2001/XMLSchema and the SOAP definition from http://schemas.xmlsoap.org/soap/envelope/.

The SOAP payload begins with . The next line opens an Execute tag that includes a URI as a parameter. This is the location of the remote system (in our case, the hotel we will be booking our reservation with). The remainder of the SOAP message contains the parameters that will be passed to the hotel, such as guest name, credit card information, etc.

As with other XML standards we reviewed, all elements must be closed out. The last three lines of the SOAP message close out the Execute, SOAP:Body, and SOAP:Envelope elements.

The intended recipient of a SOAP message is a Web Service, which supplements a Web server. When a new SOAP message comes in, the Web Service automatically parses through the message and sends the parameters to an object that has the business logic programmed into it. This object could update database tables, check availability of a room, call out to external credit card authorization companies, or anything else that is programmatically possible. In our example, we are going to check the availability of the room and then confirm that the funds are available on the credit card. Once this is complete, we will return the following XML to the program that initially submitted the XML, which is waiting for a response.

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 454


http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd="http://www.w3.org/2001/XMLSchema

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  
    
      TRUE
    
  

Figure 2: This SOAP message is a response to the original request.

This response looks a little different from the request because we included the HTTP headers. HTTP/1.1 200 OK indicates that the document you were posting to was found and that the operation completed successfully. An HTTP/1.1 404 File Not Found would indicate that the Web Service you are trying to publish was not found. You would see this message if you went to a URL and the document you entered was not found on the server. The remaining lines of the HTTP headers indicate the text encoding type, which matches the UTF-8 that we had in our request, and the length of the response.

We start the XML portion the same way we started the request. The XML version and encoding format is indicated. The SOAP envelope element is opened with references made to the same URIs as the request.

The SOAP body element is opened as well, and it uses an interesting parameter. The ExecuteResponse element has the same URI as the Execute element in the request. This is because the same Web Service that is accepting the request is then creating the response.

WSDL

First and foremost, WSDL defines a Web Service. In order to define this Web Service, it needs to describe the "binding," or what type of message to send and where (remember, a Web Service can operate from email). It also defines the port and the message that will traverse the port. A message is a series of parameters to be either received or sent, and the WSDL defines the specific parameters in a message and the format of those parameters.

This sounds both somewhat difficult and easy at the same time. You can see the hierarchy from the definition I provided above, but coming up with the syntax may be tedious. This is why you would almost never define WSDL yourself. Many packages, including C#, provide the run-time engines that create the WSDL for you. Let's look at an example that would define the reservation service that our SOAP message would be sent to.


http://schemas.xmlsoap.org/wsdl/http/" 

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/

xmlns:s="http://www.w3.org/2001/XMLSchema

xmlns:s0="http://192.168.1.1/ReservationPortal/

xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/

xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/

xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/

targetNamespace="http://192.168.1.1/ReservationPortal/

xmlns="http://schemas.xmlsoap.org/wsdl/">
  
    

targetNamespace="http://192.168.1.1/ReservationPortal/">
      
        
          
    
            
            
            
            
            
            
            
            
            
          
        
      
      
        
          
            

type="s:string" />
          
        
      
      
    
  
  
    
  
  
    
  
  
    
      
      
    
  
  
    
    
      http://192.168.1.1/ReservationPortal/ReserveRoom" 

style="document" />
      
        
      
      
        
      
    
  
  
    
      
    
  

Figure 3: This code defines the reservation service that our SOAP message is sent to.

As in our SOAP requests, the first section relates to the version of XML we are using and the encoding method. We then reference external naming schemas, including one of our own located at http://192.168.1.1/ReservationPortal/.

In WSDL, the subcomponents are defined first and the Web Service itself is defined last. The first definition is a schema. The first schema we define is ReserveRoom, a series of variables required to reserve a room. Look back at the SOAP example, and you'll see that the variable names are slightly different, but the schema definition matches what we are sending via our SOAP request.

The minOccurs and maxOccurs tags in the schema allow you to define how many times an element is present. Setting both of these values to "1" mandates that this variable be present only once. We could complicate this WSDL by adding a GuestNames tag that could have one to 100 names, depending upon the size of the room. So your minOccurs would be "1" and your maxOccurs would be up to "100."

In this WSDL, we define a schema for both the input and the output. This allows us to "code it once, call it twice" as my COBOL instructor used to say.

Moving on down the example, we find the Message element, a simple element that indicates we are using the ReserveRoom schema for our ReserveRoomSoapIn message and the ReserveRoomResponse schema for our ReserveRoomSoapOut message. So now we have bound a schema to a message, but we're not done yet.

The next step in the WSDL is defining the ports that may be available for a Web Service. A complex Web Service would have many different ports. In our simple example, we kept the port to simply reserving a room, or ReserveRoom. As you can see from the example, the input message (SOAP in our case, as opposed to email) expects ReserveRoomSoapIn, and we will be outputting ReserveRoomSoapOut. You should recognize both of these from when we defined them as messages.

The binding area is complicated, as it defines what we expect and the order in which we expect it. Because you can have several SOAP messages that constitute a Web Service transaction, the binding indicates when and how to expect those message and in what format. It also describes what you should expect to be returned, which is important if there are several messages being sent to you.

Finally, we define the Web Service itself, including the address, and reference the binding that we just defined. I don't think there is any question as to why you would want an application to automatically generate the WSDL.

UDDI

As explained in the last article, UDDI provides a searchable directory of companies that upload their business classification information. UDDI encompasses uploading the information as well as searching it.

We're going to step away from the hotel reservation and perform a simple query that looks for information on Microsoft. Here's the code:

UddiConnection CONNECTION = new UddiConnection      (http://test.uddi.microsoft.com/inquire);
FindBusiness BUSINESS = new FindBusiness("Microsoft");
BUSINESS.FindQualifiers.Add(FindQualifier.CaseSensitiveMatch);
BUSINESS.CategoryBag.Add(CommonCanonical.NtisGovNaics1997,"51121","NAICS: Software Publisher");
BusinessList BUSINESSLIST = fbBUSINESS Send(CONNECTION);

This code excerpt is from C#, and as you can see, it is relatively easy to comprehend. The first line defines a new UDDI connection to the Microsoft Test server, which is specified by the URL. The second line of code stipulates the search criteria that we are looking for. In this example, we are looking for a business named Microsoft. The third line allows us to stipulate a series of qualifiers. We want to ensure that our search is case-sensitive and does not find "microSOFT." The fourth line hones in on the business classification standard (NtisGovNaics1997) and stipulates that we are looking for a software publisher. The final line initiates the connection and retrieves the information into the BusinessList object for your use.

Relatively simple, isn't it? Of course, the devil is in the details and what you do with that information once you have it. In our example, we know the company name that we are looking for. But what if we don't, and we are looking for a type of business? How can we discern the good from the bad and the ugly? And will we even be able to do this when we search for Hotel and receive hundreds of results?

The Future of XML

There will be many degrees of XML acceptance. At present, I see the landscape of XML as a collection of islands linked by shallow narrows that anyone can walk across at will. At each island is a different XML technology that solves a different problem. How many islands you travel to depends upon your ambitions and your specific business problems.

On the fringe of these islands, a super island will be established that has bridges and a hefty admission fee. These will come to you in the form of frameworks or add-ons to your business applications. They'll carry a significant cost but will be all-encompassing. Instead of studying standards and downloading SDKs and toolkits, you'll be able to go to the super island, which will provide full XML integration with all the functionality that the standards define.

There will also be an isolated island of third-party vendors. They will operate the Web Services, perform the UDDI registration and searching, define the WSDL to use your Web Services, and transport the SOAP messages to complete complex transactions. Aside from transaction and subscription fees, the process will be relatively painless, and you'll never have to leave your island.

Nonetheless, you will most likely have to get your feet wet with XML. It is a mutating technology that seems to be touching everything that is front and center right now. You now have a better understanding of XML and the superfluous technologies that extend it so that, when the time comes, you do not drown.

Additional Resources

Since publishing the first article in this series, MC Press Online has exploded with excellent in-depth articles on XML and its surrounding technologies. In addition to these articles, author Steve Bos, whose code has been introduced here, has a full-length book in the works. Expect to see it available at the MC Press store around May. This book will provide more details on the technologies introduced here and apply them to the iSeries.

Chris Green is a Senior Network Support Specialist located in Toronto, Ontario, Canada. He has eight years experience focusing on the iSeries and networking technologies. Utilizing this experience, he has authored over 30 articles and several white papers and has co-authored an IBM Redbook entitled Securing Your AS/400 from Harm on the Internet. Most recently, he has achieved Security Essentials (GSEC) certification from the SANS Institute. For questions or comments, you can email him at This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS