- Sams Teach Yourself XML in 21 Days, Third Edition
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- All About Markup Languages
- All About XML
- Looking at XML in a Browser
- Working with XML Data Yourself
- Structuring Your Data
- Creating Well-Formed XML Documents
- Creating Valid XML Documents
- How XML Is Used in the Real World
- Online XML Resources
- Summary
- Q&A
- Workshop
- Day 2. Creating XML Documents
- Choosing an XML Editor
- Using XML Browsers
- Using XML Validators
- Creating XML Documents Piece by Piece
- Creating Prologs
- Creating an XML Declaration
- Creating XML Comments
- Creating Processing Instructions
- Creating Tags and Elements
- Creating CDATA Sections
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 3. Creating Well-Formed XML Documents
- What Makes an XML Document Well-Formed?
- Creating an Example XML Document
- Understanding the Well-Formedness Constraints
- Using XML Namespaces
- Understanding XML Infosets
- Understanding Canonical XML
- Summary
- Q&A
- Workshop
- Day 4. Creating Valid XML Documents: DTDs
- All About DTDs
- Validating a Document by Using a DTD
- Creating Element Content Models
- Commenting a DTD
- Supporting External DTDs
- Handling Namespaces in DTDs
- Summary
- Q&A
- Workshop
- Declaring Attributes in DTDs
- Day 5. Handling Attributes and Entities in DTDs
- Specifying Default Values
- Specifying Attribute Types
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 6. Creating Valid XML Documents: XML Schemas
- Using XML Schema Tools
- Creating XML Schemas
- Dissecting an XML Schema
- The Built-in XML Schema Elements
- Creating Elements and Types
- Specifying a Number of Elements
- Specifying Element Default Values
- Creating Attributes
- Summary
- Q&A
- Workshop
- Day 7. Creating Types in XML Schemas
- Restricting Simple Types by Using XML Schema Facets
- Creating XML Schema Choices
- Using Anonymous Type Definitions
- Declaring Empty Elements
- Declaring Mixed-Content Elements
- Grouping Elements Together
- Grouping Attributes Together
- Declaring all Groups
- Handling Namespaces in Schemas
- Annotating an XML Schema
- Summary
- Q&A
- Workshop
- Part I. In Review
- Well-Formed Documents
- Valid Documents
- Part II: At a Glance
- Day 8. Formatting XML by Using Cascading Style Sheets
- Our Sample XML Document
- Introducing CSS
- Connecting CSS Style Sheets and XML Documents
- Creating Style Sheet Selectors
- Using Inline Styles
- Creating Style Rule Specifications in Style Sheets
- Summary
- Q&A
- Workshop
- Day 9. Formatting XML by Using XSLT
- Introducing XSLT
- Transforming XML by Using XSLT
- Writing XSLT Style Sheets
- Using <xsl:apply-templates>
- Using <xsl:value-of> and <xsl:for-each>
- Matching Nodes by Using the match Attribute
- Working with the select Attribute and XPath
- Using <xsl:copy>
- Using <xsl:if>
- Using <xsl:choose>
- Specifying the Output Document Type
- Summary
- Q&A
- Workshop
- Day 10. Working with XSL Formatting Objects
- Introducing XSL-FO
- Using XSL-FO
- Using XSL Formatting Objects and Properties
- Building an XSL-FO Document
- Handling Inline Formatting
- Formatting Lists
- Formatting Tables
- Summary
- Q&A
- Workshop
- Part II. In Review
- Using CSS
- Using XSLT
- Using XSL-FO
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Why XHTML?
- Writing XHTML Documents
- Validating XHTML Documents
- The Basic XHTML Elements
- Organizing Text
- Formatting Text
- Selecting Fonts: <font>
- Comments: <!-->
- Summary
- Q&A
- Workshop
- Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Introducing SVG
- Creating an SVG Document
- Creating Rectangles
- Adobe's SVG Viewer
- Using CSS Styles
- Creating Circles
- Creating Ellipses
- Creating Lines
- Creating Polylines
- Creating Polygons
- Creating Text
- Creating Gradients
- Creating Paths
- Creating Text Paths
- Creating Groups and Transformations
- Creating Animation
- Creating Links
- Creating Scripts
- Embedding SVG in HTML
- Introducing SMIL
- Summary
- Q&A
- Workshop
- Day 14. Handling XLinks, XPointers, and XForms
- Introducing XLinks
- Beyond Simple XLinks
- Introducing XPointers
- Introducing XBase
- Introducing XForms
- Summary
- Workshop
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Introducing the W3C DOM
- Introducing the DOM Objects
- Working with the XML DOM in JavaScript
- Searching for Elements by Name
- Reading Attribute Values
- Getting All XML Data from a Document
- Validating XML Documents by Using DTDs
- Summary
- Q&A
- Workshop
- Day 16. Using Java and .NET: DOM
- Using Java to Read XML Data
- Finding Elements by Name
- Creating an XML Browser by Using Java
- Navigating Through XML Documents
- Writing XML by Using Java
- Summary
- Q&A
- Workshop
- Day 17. Using Java and .NET: SAX
- An Overview of SAX
- Using SAX
- Using SAX to Find Elements by Name
- Creating an XML Browser by Using Java and SAX
- Navigating Through XML Documents by Using SAX
- Writing XML by Using Java and SAX
- Summary
- Q&A
- Workshop
- Day 18. Working with SOAP and RDF
- Introducing SOAP
- A SOAP Example in .NET
- A SOAP Example in Java
- Introducing RDF
- Summary
- Q&A
- Workshop
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- XML, Databases, and ASP
- Storing Databases as XML
- Using XPath with a Database
- Introducing XQuery
- Summary
- Q&A
- Workshop
- Day 21. Handling XML in .NET
- Creating and Editing an XML Document in .NET
- From XML to Databases and Back
- Reading and Writing XML in .NET Code
- Using XML Controls to Display Formatted XML
- Creating XML Web Services
- Summary
- Q&A
- Workshop
- Part V. In Review
- Appendix A. Quiz Answers
- Quiz Answers for Day 1
- Quiz Answers for Day 2
- Quiz Answers for Day 3
- Quiz Answers for Day 4
- Quiz Answers for Day 5
- Quiz Answers for Day 6
- Quiz Answers for Day 7
- Quiz Answers for Day 8
- Quiz Answers for Day 9
- Quiz Answers for Day 10
- Quiz Answers for Day 11
- Quiz Answers for Day 12
- Quiz Answers for Day 13
- Quiz Answers for Day 14
- Quiz Answers for Day 15
- Quiz Answers for Day 16
- Quiz Answers for Day 17
- Quiz Answers for Day 18
- Quiz Answers for Day 19
- Quiz Answers for Day 20
- Quiz Answers for Day 21
A SOAP Example in .NET
You'll start our look at SOAP examples with Microsoft's .NET. In particular, you'll use Microsoft's C# language in Windows. It's a little-known fact that you can get C# for free—to use the C# command-line compiler, csc, all you need to do is install the .NET Framework's Software Development Kit (SDK), which you can get for free at http://msdn.microsoft.com/downloads.
After you install this download, you can find the csc command-line compiler in the system's root directory; for example, in Windows 2000, that is C:\WINNT\Microsoft.NET\Framework\vxxxxxxxx\csc, where xxxxxxxx is the version of the .NET framework that you downloaded. You can either add csc to your computer's path so that you can type it directly at the command line or enter its full path (that is, C:\WINNT\Microsoft.NET\Framework\vxxxxxxxx\csc) each time you run it.
You need to know how to use C# for this example. Here, you'll create a SOAP server that will send a message to a client. In this case, you'll use SOAP to send information from the server to the client so that the client will know how to call a method named upper in the server, which the client will use to capitalize some text. To describe upper, you'll create a C# interface named IUpper, which is shown in Listing 18.1.
Example 18.1. A SOAP Server (ch18_01.cs)
public interface IUpper { string upper(string text); }
You can compile ch18_01.cs into its own DLL, ch18_01.dll, like this:
%csc /t:library ch18_01. cs
This creates ch18_01.dll, which gives you a consistent way of describing the upper method to both the client and the server.
Creating a SOAP Server
In the server, you'll implement the upper method so it can read the text the client sends you in a class named Capitalizer, capitalize that text by using the Java String class's ToUpper method, and send it back to the client. You do that like this:
using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Serialization.Formatters.Soap; public class Capitalizer : MarshalByRefObject, IUpper { public string upper(string inText) { System.Console.WriteLine("Read this: \"{0}\"", inText); string outText = inText.ToUpper(); System.Console.WriteLine("Sending this: \"{0}\"", outText); return outText; } }
To actually send SOAP messages to the client, you'll use an object of the SoapFormatter class. In particular, you're going to let the client know how to access an object of your new Capitalizer class. Here's what that looks like in the server's main class:
public class ch18_02 { public static void Main() { HttpChannel channel = new HttpChannel(65111); ChannelServices.RegisterChannel(channel); Capitalizer capitalizer = new Capitalizer(); ObjRef objref = RemotingServices.Marshal(capitalizer); FileStream filestream = new FileStream("soap.txt", FileMode.Create); SoapFormatter soapformatter = new SoapFormatter(); soapformatter.Serialize(filestream, objref); filestream.Close(); System.Console.WriteLine("soap.txt created. Press Enter to quit."); System.Console.ReadLine(); } }
In this case, you're writing a SOAP message that describes the upper method in a file, soap.txt, which the client will read and use. Listing 18.2 shows the full code for the server.
Example 18.2. A SOAP Server (ch18_02.cs)
using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Serialization.Formatters.Soap; public class Capitalizer : MarshalByRefObject, IUpper { public string upper(string inText) { System.Console.WriteLine("Read this: \"{0}\"", inText); string outText = inText.ToUpper(); System.Console.WriteLine("Sending this: \"{0}\"", outText); return outText; } } public class ch18_02 { public static void Main() { HttpChannel channel = new HttpChannel(65111); ChannelServices.RegisterChannel(channel); Capitalizer capitalizer = new Capitalizer(); ObjRef objref = RemotingServices.Marshal(capitalizer); FileStream filestream = new FileStream("soap.txt", FileMode.Create); SoapFormatter soapformatter = new SoapFormatter(); soapformatter.Serialize(filestream, objref); filestream.Close(); System.Console.WriteLine("soap.txt created. Press Enter to quit."); System.Console.ReadLine(); } }
You can create the server, ch18_02.exe, by linking in the DLL file like this:
%csc /t:exe /r:ch18_01.dll ch18_02. cs
Creating a SOAP Client
In the client, you can read the SOAP message in soap.txt and use it to connect to the server. To do that, we'll need a SoapFormatter object, which we create and use to read in soap.txt like this:
using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Serialization.Formatters.Soap; public class ch18_03 { public static void Main() { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); FileStream filestream = new FileStream ("soap.txt", FileMode.Open); SoapFormatter soapformatter = new SoapFormatter(); . . .
Now use the SoapFormatter object to create an IUpper object named capper, and call the upper method of this object—which will call the upper method in the server remotely—to capitalize the text "Hello there.". Here's what it looks like in code:
using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Serialization.Formatters.Soap; public class ch18_03 { public static void Main() { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); FileStream filestream = new FileStream ("soap.txt", FileMode.Open) ; SoapFormatter soapformatter = new SoapFormatter(); try { IUpper capper = (IUpper)soapformatter.Deserialize(filestream); string outText = "Hello there."; System.Console.WriteLine("Sending this: \"{0}\"", outText); string inText = capper.upper(outText); . . . } }
Now all you need to do is display what you got back from the upper method, as seen in the client application, ch18_03.cs, shown in Listing 18.3.
Example 18.3. A SOAP Client (ch18_03.cs)
using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Serialization.Formatters.Soap; public class ch18_03 { public static void Main() { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); FileStream filestream = new FileStream ("soap.txt", FileMode.Open); SoapFormatter soapformatter = new SoapFormatter(); try { IUpper capper = (IUpper)soapformatter.Deserialize(filestream); string outText = "Hello there."; System.Console.WriteLine("Sending this: \"{0}\"", outText) ; string inText = capper.upper(outText); System.Console.WriteLine("Read this: \"{0}\"", inText); } catch(System.Exception e) { System.Console.WriteLine(e.Message); } } }
You can create the client, ch18_03.exe, like this:
%csc /t:exe /r:ch18_01.dll ch18_03. cs
Using the Server and Client
You're ready to use your two applications and let them communicate with a SOAP message. Start the server, ch18_02.exe, in one MS-DOS window, and it displays this text:
%ch18_02 soap.txt created. Press Enter to quit.
The SOAP message, which tells the client how to connect to the server and use the upper method, has now been written to the soap.txt file. To read that SOAP message, start the client application in another MS-DOS window. The client then sends the text "Hello there." to the server and gets that string back, capitalized:
%ch18_03 Sending this text: "Hello there." Got this text back: "HELLO THERE."
The server also indicates what text it got and what text it sent back to the client:
%ch18_02 soap.txt created. Press Enter to quit. Read this: "Hello there." Sending this: "HELLO THERE."
Here's the actual SOAP message that lets the server remotely connect to the upper method in the server:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas .microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org /soap/encoding/"> <SOAP-ENV:Body> <a1:ObjRef id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/ns/System.Runtime.Remoting"> <uri id="ref-2">/23186ad8_d9e2_4018_bcf1_ 82cb2b4084c2/UgL21NCXPtdS5ON7digxzBfC_1.rem</uri> <objrefFlags>0</objrefFlags> <typeInfo href="#ref-3"/> <envoyInfo xsi:null="1"/> <channelInfo href="#ref-4"/> </a1:ObjRef> <a1:TypeInfo id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/ns/System.Runtime.Remoting"> <serverType id="ref-5">Capitalizer, ch18_02, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null</serverType> <serverHierarchy xsi:null="1"/> <interfacesImplemented href="#ref-6"/> </a1:TypeInfo> <a1:ChannelInfo id="ref-4" xmlns:a1="http://schemas.microsoft.com/clr/ns/System.Runtime.Remoting"> <channelData href="#ref-7"/> </a1:ChannelInfo> <SOAP-ENC:Array id="ref-6" SOAP-ENC:arrayType="xsd:string[1]"> <item id="ref-8">IUpper, ch18_01, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null</item> </SOAP-ENC:Array> <SOAP-ENC:Array id="ref-7" SOAP-ENC:arrayType="xsd:anyType[2]"> <item href="#ref-9"/> <item href="#ref-10"/> </SOAP-ENC:Array> <a3:CrossAppDomainData id="ref-9" xmlns:a3="http://schemas.microsoft.com/clr/ns/System.Runtime. Remoting.Channels"> <_ContextID>1345320</_ContextID> <_DomainID>1</_DomainID> <_processGuid id="ref-11">f0a6d250_b75d_47be_8df4_5ed8e461af6b</_processGuid> </a3:CrossAppDomainData> <a3:ChannelDataStore id="ref-10" xmlns:a3="http://schemas.microsoft.com/clr/ns/System.Runtime. Remoting.Channels"> <_channelURIs href="#ref-12"/> <_extraData xsi:null="1"/> </a3:ChannelDataStore> <SOAP-ENC:Array id="ref-12" SOAP-ENC:arrayType="xsd:string[1]"> <item id="ref-13">http://209.177.24.57:65432</item> </SOAP-ENC:Array> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
This example shows how C# and .NET use SOAP messages behind-the-scenes. Now let's take a look at a SOAP example in Java.