oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Applying "Digital Hub" Concepts to Enterprise Software Design, Part 4
Pages: 1, 2, 3, 4, 5, 6

Getting to the XML

Now we have a Java class with some methods that will suck information out of our database and transform it to an XML string. Great! Now we need to make the XML easy to view and share. Let's build a web interface that will allow us to download the XML from our web browser. At the end of the article, I will show you how to make it available through a web service, too.

First, we need to create a component that will send the XML to a web browser. Since we already have the code to generate the XML, this part will be short and sweet.

  1. Right click (or Ctrl-click) the Web Components folder in the left panel of your project and choose Add -> New File...

    Right click (or control-click) the Web Components folder in the left panel of your project and choose Add -> New File...

  2. Under the WebObjects section, choose the "Component" type and click Next.

  3. Name the file DisplayXml and check the box next to Application Server in the Targets selector. Make sure that none of the other targets are selected and click Finish.

  4. Open in the DisplayXml folder that was just created. Edit the file to match this:

import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;

public class DisplayXml extends WOComponent {
  public DisplayXml(WOContext context) {

  public String theXml()
    return XML.buildCompleteXml();

  //Allows Internet Explorer to
  //parse as XML (for nice display)
  public void appendToResponse(WOResponse response,
                               WOContext context)
    super.appendToResponse(response, context);
    response.setHeader("text/xml", "Content-type");

We added two methods to The first is called theXml, which grabs the XML from the class that we built earlier and returns it as a string. The second method is appendToResponse, which will add some metadata to the XML as it passes to a web browser so that the browser knows it is receiving XML instead of HTML. This will allow Internet Explorer to display the data using its built-in XML parser.

Now find and edit the DisplayXml.html file. You should be able to find it in the DisplayXml.wo subfolder. We want the contents of this file to be:


If you are having trouble finding DisplayXml.html, check out the left side of this screenshot:

That is all! We are not creating an HTML page, so there is no html tag or body tag. Only a WEBOBJECT tag that tells WebObjects to insert some dynamic content when this page is requested. The dynamic content that we want to insert is our XML. The next thing we need to do is map this WEBOBJECT tag with a local Java method that returns a string. Do this now by changing the content of the Display.wod file to the following:

String1: WOString {
    value = theXml;
    escapeHTML = false;

The .wod files in WebObjects connect the HTML files to the Java files. In this one, we are telling WebObjects to substitute the value returned by the theXml method for the WEBOBJECT tag named String1. We also set escapeHTML to false because we do not want WebObjects to substitute escape characters for the special characters in our XML.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow