列印org.eclipse.xsd.XSDSchema對象

來源:互聯網
上載者:User

標籤:eclipse xsd   xsdschema   

因為網上關於Eclipse XSD的中文資料比較少,但是有的時候,我們需要使用Eclipse XSD的API去構造或者修改一個XSD檔案。

那麼當我們建立了org.eclipse.xsd.XSDSchema的對象,並已經在裡面添加或者修改許多的元素類型等資訊後,我們想知道我們的添加或者修改是否有效。

那麼這個時候我們應該怎麼辦呢?有兩種方式,我們把產生的org.eclipse.xsd.XSDSchema的對象,寫到一個檔案裡面去,另外一種方式就是直接把XSDSchema對象

轉成一個字串,然後把XSDSchema代碼的XSD列印出來。其代碼代碼方法如下:

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>");}}


列印org.eclipse.xsd.XSDSchema對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.