Because there is less information about Eclipse XSD on the web, there are times when we need to construct or modify an XSD file using the Eclipse XSD API.
So when we create a Org.eclipse.xsd.XSDSchema object and have added or modified many of the element types in it, we want to know if our additions or modifications are valid.
So what should we do at this time? There are two ways to write the generated Org.eclipse.xsd.XSDSchema object into a file, and the other way is to direct the Xsdschema object
Turn into a string, and then print out the XSD of the Xsdschema code. Its code 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