Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » xsdconfig » [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   
   16   package org.apache.xmlbeans.samples.xsdconfig;
   17   
   18   /*
   19    *This class uses the auto generated package names and class names. It doesn't consider XsdConfig file.
   20    *Note the difference between the imports in two files (CatalogXsdConfig.java and CatalogXsd.java)
   21    */
   22   
   23   import org.apache.xmlbeans.samples.catalog.CatalogDocument;
   24   import org.apache.xmlbeans.samples.catalog.JournalDocument;
   25   import org.apache.xmlbeans.samples.catalog.ArticleDocument;
   26   import org.apache.xmlbeans.samples.catalog.AVeryLongDescriptionElementDocument;
   27   
   28   import org.apache.xmlbeans.XmlException;
   29   import org.apache.xmlbeans.XmlError;
   30   import org.apache.xmlbeans.XmlObject;
   31   import org.apache.xmlbeans.XmlOptions;
   32   
   33   import java.util.ArrayList;
   34   import java.io.IOException;
   35   import java.io.File;
   36   
   37   public class CatalogXsd
   38   {
   39   
   40       public static void main(String[] args)
   41       {
   42           // Create an instance of this class to work with.
   43           CatalogXsd catalogxsd = new CatalogXsd();
   44   
   45           // Create an instance of a type based on the received XML's schema
   46           CatalogDocument catdoc = catalogxsd.parseXml(args[0]);
   47   
   48           //Prints the element values from the XML.
   49           catalogxsd.printElements(catdoc);
   50   
   51       }
   52   
   53       /**
   54        * Creates a File from the XML path provided in main arguments, then
   55        * parses the file's contents into a type (CatalogDocument) generated from schema.
   56        *
   57        * @param xmlFilePath A path to XML based on the schema in EasyPo.xsd
   58        * @return An instance of a generated schema type (CatalogDocument) that contains the
   59        *         parsed XML.
   60        */
   61       public CatalogDocument parseXml(String xmlFilePath)
   62       {
   63           File xmlfile = new File(xmlFilePath);
   64           CatalogDocument catdoc = null;
   65   
   66           try
   67           {
   68               catdoc = CatalogDocument.Factory.parse(xmlfile);
   69           }
   70           catch (XmlException e)
   71           {
   72               e.printStackTrace();
   73           }
   74           catch (IOException e)
   75           {
   76               e.printStackTrace();
   77           }
   78           return catdoc;
   79       }
   80   
   81   
   82       /**
   83        * This method prints all the element values in the given XML document based on Catalog.xsd
   84        */
   85       public void printElements(CatalogDocument catdoc)
   86       {
   87           // Get object reference of root element.
   88           CatalogDocument.Catalog catalogelement = catdoc.getCatalog();
   89   
   90           //Get all <journal> element from the root element.
   91           JournalDocument.Journal[] journalarray = catalogelement.getJournalArray();
   92   
   93           //Loop through <journal> element array.
   94           for (int i = 0; i < journalarray.length; i++)
   95           {
   96   
   97               //Retrieve all <article> elements within each <journal> element
   98               ArticleDocument.Article[] articlearray = journalarray[i].getArticleArray();
   99   
  100               //Loop through <article> array retrieved above
  101               for (int j = 0; j < articlearray.length; j++)
  102               {
  103                   System.out.println(articlearray[j].getTitle());
  104   
  105                   String[] str = articlearray[j].getAuthorArray();
  106   
  107                   for (int k = 0; k < str.length; k++)
  108                       System.out.println(str[k]);
  109   
  110                   //Note the method for retrieving <forsample> element.
  111                   System.out.println(articlearray[j].getAVeryLongDescriptionElement()
  112                       .getForsample());
  113               }
  114           }
  115           System.out.println("\n\n\n");
  116       }
  117   
  118       /**
  119        * <p>Validates the XML, printing error messages when the XML is invalid. Note
  120        * that this method will properly validate any instance of a compiled schema
  121        * type because all of these types extend XmlObject.</p>
  122        * <p/>
  123        * <p>Note that in actual practice, you'll probably want to use an assertion
  124        * when validating if you want to ensure that your code doesn't pass along
  125        * invalid XML. This sample prints the generated XML whether or not it's
  126        * valid so that you can see the result in both cases.</p>
  127        *
  128        * @param xml The XML to validate.
  129        * @return <code>true</code> if the XML is valid; otherwise, <code>false</code>
  130        */
  131       public static boolean validateXml(XmlObject xml)
  132       {
  133           boolean isXmlValid = false;
  134   
  135           // A collection instance to hold validation error messages.
  136           ArrayList validationMessages = new ArrayList();
  137   
  138           // Validate the XML, collecting messages.
  139           isXmlValid = xml.validate(
  140               new XmlOptions().setErrorListener(validationMessages));
  141   
  142           // If the XML isn't valid, print the messages.
  143           if (!isXmlValid)
  144           {
  145               System.out.println("\nInvalid XML: ");
  146               for (int i = 0; i < validationMessages.size(); i++)
  147               {
  148                   XmlError error = (XmlError) validationMessages.get(i);
  149                   System.out.println(error.getMessage());
  150                   System.out.println(error.getObjectLocation());
  151               }
  152           }
  153           return isXmlValid;
  154       }
  155   }

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