Because the online information about Eclipse XSD is relatively small in Chinese, but sometimes. We need to use the Eclipse XSD API to construct or alter an XSD file.
So when we create a Org.eclipse.xsd.XSDSchema object and have added or changed many of the element types, etc. We would like to know if our affiliation or change is valid. So what should we do at this time?
There are two ways of
(1) We put the generated Org.eclipse.xsd.XSDSchema object, write to a file in the face to
(2) The second way is to directly convert the Xsdschema object into a string and then print out the XSD of the Xsdschema code.
During the debugging of our code. Of course, the other way is more convenient and fast.
Its detailed code method is as follows:
Import Org.eclipse.xsd.xsdimport;import Org.eclipse.xsd.xsdinclude;import Org.eclipse.xsd.xsdredefine;import Org.eclipse.xsd.xsdschema;import Org.eclipse.xsd.xsdschemadirective;import Org.eclipse.xsd.util.XSDResourceImpl; Import Org.w3c.dom.element;public class Schemaprintservice {public static void Printschema (Xsdschema xsdschema) { System.out.println ("<!--===== Schema composition ====="); Printdirectives ("", Xsdschema); System.out.println ("--"); System.out.println ("<!--[" + xsdschema.getschemalocation () + "]-"); Xsdschema.updateelement (); Element element = Xsdschema.getelement (); if (element! = NULL) {//Print the serialization of the model. Xsdresourceimpl.serialize (System.out, Element); }}private static void Printschemastart (Xsdschema xsdschema) {System.out.print ("<schema targetnamespace=\"); if ( Xsdschema.gettargetnamespace ()! = null) {System.out.print (Xsdschema.gettargetnamespace ());} System. Out.print ("\" schemalocation=\ ""); if (xsdschema.getschemalocation ()! = null) {System.out.print ( Xsdschema.getschemalocation ());} System.out.print ("\" > ");} private static void Printdirectives (String indent, Xsdschema Xsdschema) {System.out.print (indent);p Rintschemastart ( Xsdschema); System.out.println (); if (!xsdschema.getreferencingdirectives (). IsEmpty ()) {System.out.println (indent + "< Referencingdirectives> "); for (Xsdschemadirective xsdSchemaDirective:xsdSchema.getReferencingDirectives ()) { Xsdschema Referencingschema = Xsdschemadirective.getschema (); System.out.print (Indent + "");p Rintschemastart (Referencingschema); System.out.println (); System.out.print (Indent + ""); if (xsdschemadirective instanceof xsdimport) {xsdimport Xsdimport = (xsdimport) Xsdsch emadirective; System.out.print ("<import namespace=\"); if (Xsdimport.getnamespace ()! = null) {System.out.print ( Xsdimport.getnamespace ());} System.out.print ("\" schemalocation=\ "");} else if (xsdschemadirective instanceofXsdredefine) {System.out.print ("<redefine schemalocation=\");} else if (xsdschemadirective instanceof xsdinclude) {System.out.print ("<include schemalocation=\");} if (xsdschemadirective.getschemalocation () = null) {System.out.print (xsdschemadirective.getschemalocation ());} System.out.println ("\"/> "); System.out.println (indent + "</schema>");} System.out.println (indent + "</referencingDirectives>");} if (!xsdschema.getincorporatedversions (). IsEmpty ()) {System.out.println (indent + "<incorporatedVersions>"); For (Xsdschema incorporatedVersion:xsdSchema.getIncorporatedVersions ()) {printdirectives (indent + "", Incorporatedve rsion);} System.out.println (indent + "</incorporatedVersions>");} System.out.println (indent + "</schema>");}}
Printing Org.eclipse.xsd.XSDSchema Objects