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