Search This Site
May 12 2008 05:12 UTC | ||||||||||
Tutorials
XML ManagerQuestions
Got a question for us? Quality
Bookmarks |
Getting StartedXML Manager is a Java component that makes it easy to save and load XML data containing lots of individual data records. This includes things like RSS and Atom feeds, web service APIs from Amazon, Google and eBay, and your own data stored using in-house schemas. The thing that XML Manager makes easy is getting a list of data fields for each record out of the XML, without having to handle all of the elements individually.
You use XML Manager by calling the methods of the
com.ricebridge.xmlman.XmlManager
class. This is the main entry point to the XML Manager component. This guide will
show you some simple examples of how to use the
Update Your Classpath
For installation instructions you should read the Installation Guide.
Assuming you have unzipped the XML Manager download file into a folder of your choice, simply
copy the Loading XML Data
To load XML data using XML Manager, you create a new instance of the
To make things easy, we will use the simplest data
structure, a
Here is a complete code example. You can try this example out by compiling the
Load.java
import com.ricebridge.xmlman.XmlManager;
import com.ricebridge.xmlman.RecordSpec;
import java.util.List;
public class Load {
public static void main( String[] args ) {
XmlManager xmlman = new XmlManager();
RecordSpec rs = new RecordSpec( "/list/president",
new String[] {"name","email"} );
List data = xmlman.load( "presidents.xml", rs );
for( int pres = 0; pres < data.size(); pres++ ) {
String[] record = (String[]) data.get(pres);
System.out.println( "NAME:"+record[0]+" EMAIL:"+record[1] );
}
}
}
This class prints out the data records in the
file presidents.xml
<list>
<president>
<name>Ronald Reagan</name>
<email>gipper@whitehouse.gov</email>
</president>
<president>
<name>Theodore Roosevelt</name>
<email>teddy@whitehouse.gov</email>
</president>
<president>
<name>Dwight D. Eisenhower</name>
<email>ike@whitehouse.gov</email>
</president>
</list>
The following lines show how XmlManager xmlman = new XmlManager();
RecordSpec rs = new RecordSpec( "/list/president",
new String[] {"name","email"} );
List data = xmlman.load( "presidents.xml", rs );
All you need to do to load XML data is to create a new XmlManager class, create a RecordSpec class to describe the XML you want to pull out, and call the XmlManager.load method with the name of the XML file.
The
To pull out data records from the XML file, we specify that we are interested in all the /list/presidentThis says, inside each the list element, look for all the president elements.
To get at the data fields of each president record, we use a set of XPath expressions. These data field expressions, one for each data field, point out the XML elements that make up the data fields of the data record. Each data field XPath expression is understood to start from the record element. So the expression namepoints out the name element inside the president element.
The XML data is returned to you as a
To load your data from a file,
If your XML data does not come from a file, then you can also provide it using a
Saving XML Data
To save XML data using XML Manager, you need an instance of the
Here is a complete code example. You can try this example out by compiling the
Save.java
import com.ricebridge.xmlman.XmlManager;
import com.ricebridge.xmlman.RecordSpec;
import java.util.List;
import java.util.ArrayList;
public class Save {
public static void main( String[] args ) {
ArrayList data = new ArrayList();
data.add( new String[] { "Richard Nixon", "tricky@whitehouse.gov" } );
data.add( new String[] { "Harry S. Truman", "haberdasher@whitehouse.gov" } );
data.add( new String[] { "Thomas Jefferson", "sage@whitehouse.gov" } );
XmlManager xmlman = new XmlManager();
RecordSpec rs = new RecordSpec( "/list/president",
new String[] {"name","email"} );
xmlman.save( "more-presidents.xml", rs, data );
}
}
This class creates an XML file more-presidents.xml
<list>
<president>
<name>Richard Nixon</name>
<email>tricky@whitehouse.gov</email>
</president>
<president>
<name>Harry S. Truman</name>
<email>haberdasher@whitehouse.gov</email>
</president>
<president>
<name>Thomas Jefferson</name>
<email>sage@whitehouse.gov</email>
</president>
</list>
The following lines show how XmlManager xmlman = new XmlManager();
RecordSpec rs = new RecordSpec( "/list/president",
new String[] {"name","email"} );
xmlman.save( "more-presidents.xml", rs, data );
All you need to do to save XML data is to use an
How does the Here's how it works: The data record XPath is more-presidents.xml
<list>
<president>
<name>[NAME]</name>
<email>[EMAIL]</email>
</president>
</list>
Now we simply take each data record, and put it into the structure. Since we have defined the record data fields using: new String[]{ "name", "email" }
we know that we must provide a list of
Thus, the XML data is provided as a
To save your data to a file,
If you need to save your XML to something that is not a file, then you can also send the data to a
Importing and Exporting XML Data from a Database
If you are using XML to store data records then you will probably want to get that data into and
out of a database at some point. XML Manager makes this really easy. For this example,
assume we have a product.sqlCREATE TABLE Product ( id integer primary key, code varchar(20), name varchar(255), price integer ); The data that we want to insert into this table is in the products.xml
<database>
<product id="1">
<code>A01</code>
<name>Apple</name>
<price>99</price>
</product>
<product id="2">
<code>O01</code>
<name>Orange</name>
<price>149</price>
</product>
<product id="3">
<code>S01</code>
<name>Satsuma</name>
<price>219</price>
</product>
<product id="4">
<code>B01</code>
<name>Banana</name>
<price>29</price>
</product>
<product id="5">
<code>P01</code>
<name>Pear</name>
<price>129</price>
</product>
</database>
Both of these files are in the
In this example, we want to import this data into the Database.java
import com.ricebridge.xmlman.XmlManager;
import com.ricebridge.xmlman.RecordSpec;
import java.sql.*;
import java.util.*;
import java.io.*;
public class Database {
public static void main( String[] args ) throws Exception {
XmlManager xmlman = new XmlManager();
RecordSpec rs = new RecordSpec("/database/product",
new String[]{"@id","code","name","price"});
String dburl
= "jdbc:mysql://localhost/mydatabase?"
+ "user=username&password=password";
Driver d = (Driver) Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
DriverManager.registerDriver( d );
Connection con = DriverManager.getConnection(dburl);
PreparedStatement ps = con.prepareStatement( "INSERT INTO Product VALUES (?,?,?,?)" );
List data = xmlman.load( "products.xml", rs );
for( int line = 0; line < data.size(); line++ ) {
String[] fields = (String[]) data.get(line);
ps.setInt( 1, Integer.parseInt( fields[0] ) );
ps.setString( 2, fields[1] );
ps.setString( 3, fields[2] );
ps.setInt( 4, Integer.parseInt( fields[3] ) );
ps.executeUpdate();
}
ps = con.prepareStatement( "SELECT * FROM Product" );
ResultSet resultset = ps.executeQuery();
xmlman.saveResultSet( "products-export.xml", rs, resultset );
con.close();
}
}
The following lines show how XmlManager xmlmanager = new XmlManager();
XmlManager xmlman = new XmlManager();
RecordSpec rs = new RecordSpec("/database/product",
new String[]{"@id","code","name","price"});
...
List data = xmlman.load( "products.xml", rs );
...
xmlman.save( "products-export.xml", rs, resultset );
So to convert data in a database to and from XML format, all you need to do is use the standard JDBC API calls and the load(File,RecordSpec) and saveResultSet(File,RecordSpec,ResultSet) methods. Changing the Settings
The XML Manager component uses the XmlSpec class
to change the way that XML is handled. The For example, when loading XML, you can use the setValidating method to enable or disable validation of the XML file against a DTD or Schema (the default is not to validate).
When saving XML, you can use the
setIndent method to enable or disable
indenting of the XML file (the default is to indent). When you want to make the file as small as possible, use There are a number of other properties you can set, including the
encoding of the XML file
(default: Handling NamespacesYou will often come across XML files that use namespaces. The most common example is probably XSLT files. Let's look at a simple case and see how XML Manager handles XML namespaces. namespace.xml
<root>
<foo xmlns="http://www.ricebridge.com/first" >
<bar>bar one</bar>
</foo>
<foo xmlns="http://www.ricebridge.com/second" >
<bar>bar two</bar>
</foo>
</root>
By default, XML Manager supports namespaces. But we can turn this off. In fact, this is often a quick way to work with XML that uses namespaces. We can pretend they aren't there and carry on as normal. Here is the code to do that.
public static void main( String[] args ) {
XmlManager xmlman = new XmlManager();
xmlman.getXmlSpec().setNamespaceAware( false );
RecordSpec rs = new RecordSpec( "/root/foo", new String[] {"bar"} );
print( "no namespace", xmlman, rs );
}
/** A utility method to print the data. */
public static void print( String pMessage, XmlManager xmlman, RecordSpec rs ) {
System.out.println( pMessage );
List data = xmlman.load( "namespace.xml", rs );
for( int foo = 0; foo < data.size(); foo++ ) {
String[] record = (String[]) data.get(foo);
System.out.println( "BAR:"+record[0] );
}
}
This prints out: no namespace BAR:bar one BAR:bar two The important line to notice is: xmlman.getXmlSpec().setNamespaceAware( false ); Of course, we should deal with namespaces properly. XML Manager needs to be told in
advance about the namespaces in the XML file. You do this by calling the
XmlSpec.addNamespace
method. Here is the code (we use the
xmlman.getXmlSpec().setNamespaceAware( true );
xmlman.getXmlSpec().addNamespace( "f1", "http://www.ricebridge.com/first" );
rs = new RecordSpec( "/root/f1:foo", new String[] {"f1:bar"} );
print( "namespace: f1", xmlman, rs );
This prints out: namespace: f1 BAR:bar one Notice that in this case only the More Information
This Getting Started guide only covers the basics of what you can do with XML Manager.
For more information on settings, refer to the reference documentation.
For a list of examples, including a Swing example with
Please review the licensing terms and
conditions under which the Ricebridge XML Manager component can be
used. We're always happy to come up with licensing solutions that suit
your needs, so please contact us by mail at
Thanks for using XML Manager! If you have any problems or questions feel free to mail us at
| |||||||||
|
comment on this page
Home |
Search |
About Us |
Contact Us |
Our Products |
Documentation |
Resources |
Login
Copyright © 2004-2008 Ricebridge. All Rights Reserved. | ||||||||||