Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » xquery » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   package org.apache.xmlbeans.samples.xquery;
   16   
   17   import org.apache.xmlbeans.XmlException;
   18   import org.apache.xmlbeans.XmlObject;
   19   
   20   import java.io.File;
   21   import java.io.IOException;
   22   
   23   /**
   24    * A sample to XMLBeans API features for executing XPath and XQuery
   25    * expressions. The sample illustrates these features:
   26    * 
   27    * - Using the XmlObject.selectPath and XmlCursor.selectPath methods
   28    * to execute XPath expressions. The selectPath method's results (if
   29    * any) are always chunks of the instance queried against. In other
   30    * words, changes to query results change the original instance.
   31    * However, you work with results differently depending on whether
   32    * selectPath was called from an XmlObject or XmlCursor instance. See
   33    * the SelectPath class for more information.
   34    * - Using the XmlObject.execQuery and XmlCursor.execQuery methods
   35    * to execute XQuery expressions. Results of these queries are copied
   36    * into new XML, meaning that changes to results do not change the 
   37    * original instance. Here again, you work with results differently
   38    * depending how which method you used to query. See the ExecQuery
   39    * class for more information.
   40    */
   41   public class XQueryXPath
   42   {
   43       /**
   44        * Receives an employees list XML instance, passing the instance to
   45        * methods that execute queries against it.
   46        * 
   47        * @param args An array in which the first item is a
   48        * path to the XML instance file.
   49        */
   50       public static void main(String[] args)
   51               throws org.apache.xmlbeans.XmlException, java.io.IOException
   52       {
   53           XQueryXPath sample = new XQueryXPath();
   54           sample.executeQueries(args);
   55       }
   56       
   57       /**
   58        * Returns <code>true</code> if all of the sample methods returned true
   59        * (i.e., their query expressions returned results).
   60        * 
   61        * @param args An array in which the first item is a
   62        * path to the XML instance file.
   63        * @return <code>true</code> if all of the sample methods returned true
   64        * (i.e., their query expressions returned results); otherwise, 
   65        * <code>false</code>.
   66        */
   67       public boolean executeQueries(String[] args)
   68       {
   69           XmlObject xml = this.parseXml(args[0]);
   70           
   71           // Execute the XQuery samples.
   72           ExecQuery xquerySample = new ExecQuery();
   73           
   74           System.out.println("Running ExecQuery.selectEmpsByStateCursor\n");
   75           boolean xqWorkPhoneSuccessful = xquerySample.updateWorkPhone(xml);
   76           
   77           System.out.println("Running ExecQuery.selectZipsNewDocCursor\n");
   78           boolean xqCollectZips = xquerySample.collectZips(xml);
   79   
   80           // Execute the XPath samples.
   81           SelectPath xpathSample = new SelectPath();
   82           
   83           System.out.println("Running SelectPath.updateWorkPhone \n");
   84           boolean xpWorkPhoneSuccessful = xpathSample.updateWorkPhone(xml);
   85           
   86           System.out.println("Running SelectPath.collectNames \n");
   87           boolean xpCollectNames = xpathSample.collectNames(xml);
   88           
   89           return (xqWorkPhoneSuccessful && xqCollectZips
   90                   && xpWorkPhoneSuccessful && xpCollectNames) ? true : false;
   91       }
   92   
   93       /**
   94        * <p>Creates a File from the XML path provided in main arguments, then
   95        * parses the file's contents into a type generated from schema.</p>
   96   
   97        * @param xmlFilePath A path to XML based on the schema in inventory.xsd.
   98        * @return An instance of a generated schema type that contains the parsed
   99        *         XML.
  100        */
  101       public XmlObject parseXml(String xmlFilePath)
  102       {
  103           File xmlFile = new File(xmlFilePath);
  104           XmlObject xml = null;
  105           try
  106           {
  107               xml = XmlObject.Factory.parse(xmlFile);
  108           } catch (XmlException e)
  109           {
  110               e.printStackTrace();
  111           } catch (IOException e)
  112           {
  113               e.printStackTrace();
  114           }
  115           return xml;
  116       }
  117   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » xquery » [javadoc | source]