Because there are few Chinese documents on EclipseXSD on the Internet, but sometimes we need to use the EclipseXSD 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?
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("
"); System.out.println("
"); 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("
");}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 + "
");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("
");System.out.println(indent + "
");}System.out.println(indent + " ");}if (!xsdSchema.getIncorporatedVersions().isEmpty()) {System.out.println(indent + "
");for (XSDSchema incorporatedVersion : xsdSchema.getIncorporatedVersions()) {printDirectives(indent + " ", incorporatedVersion);}System.out.println(indent + "
");}System.out.println(indent + "");}}