Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (Français) » MobiLink - Getting Started » MobiLink Tutorials » Tutorial: Synchronizing with XML » Lesson 4: Create a Java class for MobiLink direct row handling


Complete MobiLinkOrders Java code listing

The following listing shows the complete Java MobiLinkOrders class code used for this tutorial. For a step by step explanation, see Lesson 4: Create a Java class for MobiLink direct row handling.

import java.sql.*;

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;  

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;  

public class MobiLinkOrders {

    // Class level DBConnectionContext
    DBConnectionContext _cc;
    Document _doc;
    public MobiLinkOrders( DBConnectionContext cc ) throws IOException, FileNotFoundException {
        // Declare a class-level DBConnectionContext
        _cc = cc;

    //  Method for the handle_UploadData synchronization event
    public void GetUpload( UploadData ut ) throws SQLException, IOException {
        // Get an UploadedTableData for the remote table
        UploadedTableData remoteOrdersTable = ut.getUploadedTableByName("OrderComments");

        // Get inserts uploaded by the MobiLink client 
        // as a java.sql.ResultSet    
        ResultSet insertResultSet = remoteOrdersTable.getInserts();
        // Write out each insert in the XML file
        while( ) {  

        // Close the result set of uploaded inserts
    private void buildXML( ResultSet rs ) throws SQLException {  
        int order_id = rs.getInt(1);
        int comment_id = rs.getInt(2);
        String order_comment = rs.getString(3);    

        // Create the comment object to be added to the XML file
        Element comment = _doc.createElement("comment");
        comment.setAttribute("id", Integer.toString(comment_id));

        // Get the root element (orders)
        Element root = _doc.getDocumentElement();
        // Get each individual order
        NodeList rootChildren = root.getChildNodes();
        for(int i = 0; i < rootChildren.getLength(); i++) {
            // If the order exists, add the comment to the order      
            Node n = rootChildren.item(i);
            if(n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                int idIntVal = Integer.parseInt(e.getAttribute("id"));
                if(idIntVal == order_id) {
                    // The comment has been added to the file, so exit the function

        // If the order did not exist already, create it
        Element order = _doc.createElement("order");
        order.setAttribute("id", Integer.toString(order_id));

        // Add the comment to the new order

    private void writeXML() {
        try {
            // Use a Transformer for output
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            // The XML source is _doc
            DOMSource source = new DOMSource(_doc);
            // Write the xml data to order_comments.xml
            StreamResult result = new StreamResult(new File("order_comments.xml"));
            transformer.transform(source, result);
        } catch (TransformerConfigurationException tce) {
            // Error generated by the parser
            System.out.println ("\n** Transformer Factory error");
            System.out.println("   " + tce.getMessage() );

            // Use the contained exception, if any
            Throwable x = tce;
            if (tce.getException() != null) x = tce.getException();
        } catch (TransformerException te) {
            // Error generated by the parser
            System.out.println ("\n** Transformation error");
            System.out.println("   " + te.getMessage() );

            // Use the contained exception, if any
            Throwable x = te;
            if (te.getException() != null) x = te.getException();

    private void readDom(String filename) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            //parse the Document data into _doc
            DocumentBuilder builder = factory.newDocumentBuilder();
            _doc = builder.parse( new File(filename) );
        } catch (SAXException sxe) {
            // Error generated during parsing)
            Exception x = sxe;
            if (sxe.getException() != null) x = sxe.getException();

        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built

        } catch (IOException ioe) {
            // I/O error