Search This Site
May 16 2008 05:54 UTC | |||||||||||||||||
Tutorials
XML ManagerQuestions
Got a question for us? Quality
Bookmarks |
HTML Table ExampleSummaryThis is a very simple introductory example showing you how to:
The DataWe 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:
XML Manager interprets the XPath expressions in the following manner:
To specify these XPath expressions to XML Manager, we use a new RecordSpec("/shop/product", new String[] {"@code","name","price"} ).
Loading the DataHere 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
Saving the DataWe 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 We need to create a new <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 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 CodeHere 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 CommentsPlease 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. | |||||||||||||||||