Print the org. eclipse. xsd. XSDSchema object, eclipsexsd
Because there are few Chinese documents on Eclipse XSD on the Internet, but sometimes we need to use the Eclipse xsd api to construct or modify an XSD file. When org. eclipse. xsd. after the XSDSchema object has added or modified many element types and other information in it, we want to know whether our addition or modification is valid. What should we do at this time?
There are two ways,
(1) We will write the generated org. eclipse. xsd. XSDSchema object into a file.
(2) Another way is to directly convert the XSDSchema object into a string and print the xsd of the XSDSchema code.
In our code debugging process, of course, the second method is more convenient and convenient. The Code 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);printSchemaStart(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 + " ");printSchemaStart(referencingSchema);System.out.println();System.out.print(indent + " ");if (xsdSchemaDirective instanceof XSDImport) {XSDImport xsdImport = (XSDImport) xsdSchemaDirective;System.out.print("<import namespace=\"");if (xsdImport.getNamespace() != null) {System.out.print(xsdImport.getNamespace());}System.out.print("\" schemaLocation=\"");} else if (xsdSchemaDirective instanceof XSDRedefine) {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 + " ", incorporatedVersion);}System.out.println(indent + " </incorporatedVersions>");}System.out.println(indent + "</schema>");}}