Source Code Cross Referenced for CatalogXsdConfig.java in  » J2EE » enhydra » org » apache » xmlbeans » samples » xsdconfig » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » enhydra » org.apache.xmlbeans.samples.xsdconfig 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*   Copyright 2004 The Apache Software Foundation
002:         *
003:         *   Licensed under the Apache License, Version 2.0 (the "License");
004:         *   you may not use this file except in compliance with the License.
005:         *   You may obtain a copy of the License at
006:         *
007:         *       http://www.apache.org/licenses/LICENSE-2.0
008:         *
009:         *   Unless required by applicable law or agreed to in writing, software
010:         *   distributed under the License is distributed on an "AS IS" BASIS,
011:         *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:         *   See the License for the specific language governing permissions and
013:         *  limitations under the License.
014:         */
015:
016:        package org.apache.xmlbeans.samples.xsdconfig;
017:
018:        /**
019:         *This class uses the package names and class names mentioned in XsdConfig. 
020:         *Note the difference between the imports in two files (CatalogXsdConfig.java and CatalogXsd.java)
021:         */
022:
023:        import com.catalog.XmlCatalogDocumentBean;
024:        import com.catalog.XmlJournalDocumentBean;
025:        import com.catalog.XmlArticleDocumentBean;
026:        import com.catalog.XmlShortItemBean;
027:
028:        import org.apache.xmlbeans.XmlException;
029:        import org.apache.xmlbeans.XmlError;
030:        import org.apache.xmlbeans.XmlObject;
031:        import org.apache.xmlbeans.XmlOptions;
032:
033:        import java.util.ArrayList;
034:        import java.io.IOException;
035:        import java.io.File;
036:
037:        public class CatalogXsdConfig {
038:
039:            public static void main(String[] args) {
040:                // Create an instance of this class to work with.
041:                CatalogXsdConfig catxsdconfig = new CatalogXsdConfig();
042:
043:                // Create an instance of a type based on the received XML's schema
044:                XmlCatalogDocumentBean catdoc = catxsdconfig.parseXml(args[0]);
045:
046:                //Prints the element values from the XML.
047:                catxsdconfig.printElements(catdoc);
048:            }
049:
050:            /**
051:             * Creates a File from the XML path provided in main arguments, then
052:             * parses the file's contents into a type (CatalogDocument) generated from schema.
053:             *
054:             * @param xmlFilePath A path to XML based on the schema in EasyPo.xsd
055:             * @return An instance of a generated schema type (CatalogDocument) that contains the
056:             *         parsed XML.
057:             */
058:            public XmlCatalogDocumentBean parseXml(String xmlFilePath) {
059:                File xmlfile = new File(xmlFilePath);
060:                XmlCatalogDocumentBean catdoc = null;
061:
062:                try {
063:                    catdoc = XmlCatalogDocumentBean.Factory.parse(xmlfile);
064:                } catch (XmlException e) {
065:                    e.printStackTrace();
066:                } catch (IOException e) {
067:                    e.printStackTrace();
068:                }
069:                return catdoc;
070:            }
071:
072:            /*
073:             * This method prints all the element values in the given XML document based on Catalog.xsd
074:             */
075:            public void printElements(XmlCatalogDocumentBean catdoc) {
076:                // Get object reference of root element.
077:                XmlCatalogDocumentBean.Catalog catalogelement = catdoc
078:                        .getCatalog();
079:
080:                //Get all <journal> element from the root element.
081:                XmlJournalDocumentBean.Journal[] journalarray = catalogelement
082:                        .getJournalArray();
083:
084:                //Loop through <journal> element array.
085:                for (int i = 0; i < journalarray.length; i++) {
086:
087:                    //Retrieve all <article> elements within each <journal> element
088:                    XmlArticleDocumentBean.Article[] articlearray = journalarray[i]
089:                            .getArticleArray();
090:
091:                    //Loop through <article> array retrieved above
092:                    for (int j = 0; j < articlearray.length; j++) {
093:                        System.out.println(articlearray[j].getTitle());
094:
095:                        String[] str = articlearray[j].getAuthorArray();
096:
097:                        for (int k = 0; k < str.length; k++)
098:                            System.out.println(str[k]);
099:
100:                        //Note the method for retrieving <forsample> element.
101:                        System.out.println(articlearray[j]
102:                                .getXmlShortItemBean().getGoodName());
103:
104:                    }
105:                }
106:                System.out.println("\n\n\n");
107:            }
108:
109:            /**
110:             * <p>Validates the XML, printing error messages when the XML is invalid. Note
111:             * that this method will properly validate any instance of a compiled schema
112:             * type because all of these types extend XmlObject.</p>
113:             * <p/>
114:             * <p>Note that in actual practice, you'll probably want to use an assertion
115:             * when validating if you want to ensure that your code doesn't pass along
116:             * invalid XML. This sample prints the generated XML whether or not it's
117:             * valid so that you can see the result in both cases.</p>
118:             *
119:             * @param xml The XML to validate.
120:             * @return <code>true</code> if the XML is valid; otherwise, <code>false</code>
121:             */
122:            public static boolean validateXml(XmlObject xml) {
123:                boolean isXmlValid = false;
124:
125:                // A collection instance to hold validation error messages.
126:                ArrayList validationMessages = new ArrayList();
127:
128:                // Validate the XML, collecting messages.
129:                isXmlValid = xml.validate(new XmlOptions()
130:                        .setErrorListener(validationMessages));
131:
132:                // If the XML isn't valid, print the messages.
133:                if (!isXmlValid) {
134:                    System.out.println("\nInvalid XML: ");
135:                    for (int i = 0; i < validationMessages.size(); i++) {
136:                        XmlError error = (XmlError) validationMessages.get(i);
137:                        System.out.println(error.getMessage());
138:                        System.out.println(error.getObjectLocation());
139:                    }
140:                }
141:                return isXmlValid;
142:            }
143:
144:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.