Ricebridge
Search This Site
May 16 2008 05:54 UTC

Got a question for us?
Just Ask!


$15 Gift Certificate for every bug you find.

Bookmark Ricebridge Java Components - user friendly and well documented at del.icio.us Digg Ricebridge Java Components - user friendly and well documented at Digg.com Bookmark Ricebridge Java Components - user friendly and well documented at reddit.com Bookmark Ricebridge Java Components - user friendly and well documented at YahooMyWeb Bookmark Ricebridge Java Components - user friendly and well documented at Spurl.net Bookmark Ricebridge Java Components - user friendly and well documented at Simpy.com Bookmark Polyphasic Mutants at NewsVine Blink this Ricebridge Java Components - user friendly and well documented at blinklist.com Bookmark Ricebridge Java Components - user friendly and well documented at Furl.net Fark Ricebridge Java Components - user friendly and well documented at Fark.com

HTML Table Example

Summary

This is a very simple introductory example showing you how to:

  • load XML data from a file, and
  • save XML data as an HTML table.

The Data

We will use a small XML file as the example data. This file contains the details of several products.

products.xml

<shop>
  <product code="A1">
    <price>0.10</price>
    <name>Apple</name>
  </product>
  <product code="O1">
    <price>0.20</price>
    <name>Orange</name>
  </product>
  <product code="P1">
    <price>0.30</price>
    <name>Pear</name>
  </product>
</shop>

We want to pull out the code, name and price of each product, and put them in a table. To do this we use the following XPath expressions:

  • To get each product: /shop/product
  • To get the code (relative to the product): @code
  • To get the name (relative to the product): name
  • To get the price (relative to the product): price

XML Manager interprets the XPath expressions in the following manner:

  • /shop/product: grab the contents of each product element
  • @code: get the code attribute of the product element
  • name: get the text content of the name element
  • price: get the text content of the price element

To specify these XPath expressions to XML Manager, we use a RecordSpec object, as follows:

new RecordSpec("/shop/product", new String[] {"@code","name","price"} )
.

Loading the Data

Here is the Java code to load the data:


XmlManager xmlman  = new XmlManager();
RecordSpec inputrs 
  = new RecordSpec( "/shop/product", 
                    new String[] {"@code","name","price"} );
List data = xmlman.load( "products.xml", inputrs );

This loads the data into a List of String[] arrays, where each String[] array contains the data in the XML file, in the order of the XPath expressions:

CodeNamePrice
A1 Apple 0.10
O1 Orange 0.20
P1 Pear 0.30

Saving the Data

We want to save the data to an HTML table, just like the one above. Here is the HTML source code that we want to produce:


<table><tr><th>Code</th><th>Name</th><th>Price</th><tr>
<tr>
  <td>A1</td>
  <td>Apple</td>
  <td>0.10</td>
</tr>
<tr>
  <td>O1</td>
  <td>Orange</td>
  <td>0.20</td>
</tr>
<tr>
  <td>P1</td>
  <td>Pear</td>
  <td>0.30</td>
</tr>
</table>

We want to save this to a file called table.htm (we'll drop the <html> and <body> tags for the sake of this example).

We need to create a new RecordSpec object to tell XML Manager how to create the output XML. XML Manager takes the XPath expressions and creates the XML based on the them. For example /shop/product creates the XML:

<shop>
  <product>
  </product>
</shop>

We will use the following RecordSpec to specify the output XML:

new RecordSpec("tr", new String[] { "td[1]", "td[2]", "td[3]" } )
.

This will create an HTML tr element for each product, and output an HTML td element for each field. We use the XPath syntax [n] to specify the location of the td elements (otherwise all the data would end up in a single td element).

Here is the Java code to save the data:


XmlSpec xmlspec = xmlman.getXmlSpec();
xmlspec.setHeader("<table><tr><th>Code</th><th>Name</th><th>Price</th><tr>\n");
xmlspec.setFooter("</table>");

RecordSpec outputrs = new RecordSpec("tr", new String[] { "td[1]", "td[2]", "td[3]" } );
xmlman.save( "table.htm", outputrs, data );

The Java Code

Here is the complete Java code for this example:

HtmlTable.java

/** You may copy this example and use it for any purpose, commercial or otherwise. */

import com.ricebridge.xmlman.XmlManager;
import com.ricebridge.xmlman.RecordSpec;
import com.ricebridge.xmlman.XmlSpec;

import java.util.List;


/** This is a very simple example that shows you how to load and
 *  save XML data using XML Manager. The data to load is taken from 
 *  the file "products.xml", in the same folder as this Java source 
 *  file. The saved data is written to a new file called "table.htm",
 *  also in the same folder.
 *
 *  To compile and run this program, you will need to include xmlman.jar
 *  (in the lib folder of the XML Manager distribution) in your CLASSPATH. 
 */
public class HtmlTable {

  public static void main( String[] args ) {

    // To use XML Manager, start by creating a new XmlManager object.
    XmlManager xmlman = new XmlManager();

    // Then you need a RecordSpec object.
    // This tells XML Manager what data to extract from the XML file
    RecordSpec inputrs = new RecordSpec( "/shop/product", 
                                        new String[] {"@code","name","price"} );

    // And now load the data.
    List data = xmlman.load( "products.xml", inputrs );
    
    // The data we just loaded is available as a List of String[] arrays.
    // We can loop through it manually to access it, but we can also
    // use XML Manager to output the data in a different format.

    // Get the XmlSpec object to change the output settings
    XmlSpec xmlspec = xmlman.getXmlSpec();
    xmlspec.setHeader("<table><tr><th>Code</th><th>Name</th><th>Price</th><tr>\n");
    xmlspec.setFooter("</table>");

    // Create an output RecordSpec telling XML Manager how to create the XML document
    RecordSpec outputrs = new RecordSpec("tr", new String[] { "td[1]", "td[2]", "td[3]" } );

    // And save the data to a new file.
    xmlman.save( "table.htm", outputrs, data );
  }
}

Questions and Comments

Please feel free to email us at examples@ricebridge.com if you have any questions or comments about this example.

comment on this page Home | Search | About Us | Contact Us | Our Products | Documentation | Resources | Login
Copyright © 2004-2008 Ricebridge. All Rights Reserved.