使用 eclipse 从 xsd 生成无头 xml

发布于 2024-08-05 18:13:16 字数 170 浏览 2 评论 0原文

在最新版本的免费开源 Eclipse IDE 中,您可以从 DTD 和 XSD 文件生成 XML 文档。右键单击给定的 *.dtd 或 *.xsd 文件,然后选择“生成 -> XML 文件...”。您可以选择要生成哪个根元素以及是否应生成可选属性和元素。

我可以使用这个无头(不启动 Eclipse)吗?

In recent versions of the free and open source Eclipse IDE you can generate XML documents from DTD and XSD files. Right-click on a given *.dtd or *.xsd file and select "Generate -> XML File...". You can choose which root element to generate and whether optional attributes and elements should be generated.

Can i use this headless (without starting eclipse)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-08-12 18:13:16

您可以创建一个无头 RCP 应用程序,其中仅包含执行以下操作所需的插件实际一代。这些主要是 WTP 插件,其中包含一些管理扩展点等所需的核心插件。

RCP 应用程序可以从命令行运行,并传递要生成的架构的参数和输出文件名。它缺少您在生产实现中可能需要的大部分验证,但向您展示了如何完成它。
它还将字符集硬编码为 UTF-8,您可以扩展参数处理以使其成为可选参数或其他参数。

下面的代码片段可以合并到新的无头 RCP 应用程序中。要创建RCP应用程序,首先创建一个新的Plugin项目:

  • 右键->新建->其他...->插件开发->插件项目,选择下一步
  • 输入项目名称(例如name.seller.rich.xmlgen)并选择下一步
  • 取消选中此插件将对UI做出贡献富客户端应用程序下选择,然后单击完成
  • 要添加所需的依赖项,请双击 META-INF/Manifest.MF并选择编辑器的依赖项选项卡,将以下插件添加到所需插件部分(单击“添加...”并添加每个插件)
    • org.eclipse.core.runtime,
    • org.eclipse.core.resources;bundle-version="3.5.0",
    • org.eclipse.wst.common.uriresolver;bundle-version="1.1.301",
    • org.eclipse.wst.sse.core;bundle-version="1.1.400",
    • org.eclipse.wst.xml.core;bundle-version="1.1.400",
    • org.eclipse.wst.xml.ui;bundle-version="1.1.0",
    • org.eclipse.xsd;bundle-version="2.5.0",
    • com.ibm.icu;bundle-version="4.0.1",
    • org.eclipse.wst.xsd.core;bundle-version="1.1.401",
    • org.eclipse.wst.xsd.ui;bundle-version="1.2.204",
    • org.eclipse.emf.ecore;bundle-version="2.5.0"
  • < p>在项目中,您应该看到一个 Application 类,将下面的 Java 内容复制到 Application 源的 start() 方法中(并且导入到文件顶部)。

    导入java.io.ByteArrayOutputStream;
    导入java.io.File;
    导入 java.io.FileInputStream;
    导入 java.io.FileOutputStream;
    导入 java.io.FileWriter;
    导入java.net.URI;
    导入java.net.URL;

    导入 org.eclipse.core.internal.utils.FileUtil;
    导入 org.eclipse.core.runtime.Platform;
    导入 org.eclipse.emf.ecore.plugin.EcorePlugin;
    导入 org.eclipse.equinox.app.IApplication;
    导入 org.eclipse.equinox.app.IApplicationContext;
    导入 org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
    导入 org.eclipse.wst.xml.ui.internal.wizards.NewXMLGenerator;

    公共对象开始(IApplicationContext上下文)抛出异常{
    String[] args = Platform.getCommandLineArgs();

    String schemaFileName = args[0];// 例如“C:\test\test.xsd”
    String xmlFileName = args[1];// 例如“C:\test\test.xml”
    String rootName = args[2];//"myTestRoot";
    字符串字符集名称 = "UTF-8";
    
    尝试 {
        //获取完整URL路径形式的URI
        URI schemaUri = new File(schemaFileName).toURI();
        schemaFileName = schemaUri.toURL().toString();
    
        //TODO 处理设置到该数组中的任何错误信息
        String[] errorInfo = new String[2];
        CMDocument cmDocument = NewXMLGenerator.createCMDocument(schemaFileName,
                错误信息);
        NewXMLGenerator 生成器 = new NewXMLGenerator(schemaFileName,
                厘米文档);
    
        生成器.setRootElementName(rootName);
    
        ByteArrayOutputStream out = Generator.createXMLDocument(xmlFileName,
                字符集名称);
    
        //将内容输出到文件中。
        文件outFile = 新文件(xmlFileName);
    
        outFile.getParentFile().mkdirs();
    
        FileWriter writer = new FileWriter(outFile);
    
        writer.write(out.toString(charsetName));
    
        writer.flush();
        writer.close();
    } catch (异常 e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    
    返回IApplication.EXIT_OK;
    

    }

创建独立应用程序,您还需要创建产品配置

  • 右键单击->新建->其他...->插件开发->产品配置
  • 选择 RCP 插件项目
  • 在文件名:字段中输入config,然后单击完成。
  • 在 config.product 编辑器 ID、版本和名称字段中输入一些合适的值(它们并不重要,因为它是无头产品)。
  • 产品定义部分中,选择产品:字段旁边的新建...按钮,默认值应该没问题(仔细检查< strong>定义插件是您的 RCP 插件),选择“确定”
  • 保存产品

您现在需要导出 RCP 应用程序。

  • 右键单击项目 -> 导出... -> 插件开发 -> Eclipse 产品
  • 输入应用程序的目标目录,然后选择确定

您现在应该有一个独立的应用程序您可以像调用任何其他应用程序一样调用,传递命令行参数以从架构生成 XML 文件。

预期参数按顺序为:

  • 要生成的模式的完全限定路径 要
  • 创建的文件的完全限定名称(还将创建父目录)。
  • 根元素名称(与向导中相同,这是您要在下面生成内容的元素的名称)。

注意此过程只会从单个架构生成文件,如果您的架构引用其他架构,当前将会失败。可以扩展该流程以获取列出所有引用模式位置的属性文件,并将这些文件解析为目录贡献,以便该流程可以解析模式。下面是关于如何以及为何执行此操作的一些说明。
如果有机会,我会考虑实施这一点并相应地更新我的答案。


例如,如果您有一个 Spring 模式,您可能希望在模式文件中包含各种 Spring 命名空间模式。在 Eclipse 中,目录贡献提供了一种将这些模式 ID 映射到模式文件位置的方法,以便可以对其进行解析。如果您有它们的插件,它们可以与应用程序捆绑在一起并定义目录贡献(请参阅 帮助 有关贡献它们的指示)。

如果您没有可用的目录贡献,该过程将改为在属性文件中定义键值对以引用驱动器上的架构位置。

示例内容:

 http://www.springframework.org/schema/beans=c:\\schema\\spring-beans.xsd
 http://www.springframework.org/schema/tool=c:\\schema\\spring-tool.xsd

You can create a headless RCP application that contains only those plugins needed to do the actual generation. These are largely WTP plugins with a couple of the core plugins needed for managing extension points and such.

The RCP app can be run from the command line, and passed arguments for the schema to generate from and the output file name. It is missing much of the validation you might want in a production implementation, but shows you how it can be done.
It also hardcodes the charset to UTF-8, you can extend the argument processing to make that an optional parameter or something.

The snippets below can be incorporated into a new headless RCP application. To create the RCP application, first create a new Plugin project:

  • Right-click->New->Other...->Plug-in Development->Plug-in Project, select Next
  • Enter a name for the project (e.g. name.seller.rich.xmlgen) and select Next
  • Uncheck This plug-in will make contributions to the UI and select Yes under Rich Client Application then click Finish
  • To add the required dependencies, double-click on META-INF/Manifest.MF and select the Dependencies tab of the editor add the following plugins to the Required Plug-ins section (click on Add... and add each one)
    • org.eclipse.core.runtime,
    • org.eclipse.core.resources;bundle-version="3.5.0",
    • org.eclipse.wst.common.uriresolver;bundle-version="1.1.301",
    • org.eclipse.wst.sse.core;bundle-version="1.1.400",
    • org.eclipse.wst.xml.core;bundle-version="1.1.400",
    • org.eclipse.wst.xml.ui;bundle-version="1.1.0",
    • org.eclipse.xsd;bundle-version="2.5.0",
    • com.ibm.icu;bundle-version="4.0.1",
    • org.eclipse.wst.xsd.core;bundle-version="1.1.401",
    • org.eclipse.wst.xsd.ui;bundle-version="1.2.204",
    • org.eclipse.emf.ecore;bundle-version="2.5.0"
  • In the project you should see an Application class, copy the Java content below into the start() method of the Application source (and the imports to the top of the file).

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.net.URI;
    import java.net.URL;

    import org.eclipse.core.internal.utils.FileUtil;
    import org.eclipse.core.runtime.Platform;
    import org.eclipse.emf.ecore.plugin.EcorePlugin;
    import org.eclipse.equinox.app.IApplication;
    import org.eclipse.equinox.app.IApplicationContext;
    import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
    import org.eclipse.wst.xml.ui.internal.wizards.NewXMLGenerator;

    public Object start(IApplicationContext context) throws Exception {
    String[] args = Platform.getCommandLineArgs();

    String schemaFileName = args[0];// e.g. "C:\test\test.xsd"
    String xmlFileName = args[1];// e.g. "C:\test\test.xml"
    String rootName = args[2];//"myTestRoot";
    String charsetName = "UTF-8";
    
    try {
        //get the URI as a full URL path
        URI schemaUri = new File(schemaFileName).toURI();
        schemaFileName = schemaUri.toURL().toString();
    
        //TODO handle any errorInfo set into this array
        String[] errorInfo = new String[2];
        CMDocument cmDocument = NewXMLGenerator.createCMDocument(schemaFileName,
                errorInfo);
        NewXMLGenerator generator = new NewXMLGenerator(schemaFileName,
                cmDocument);
    
        generator.setRootElementName(rootName);
    
        ByteArrayOutputStream out = generator.createXMLDocument(xmlFileName,
                charsetName);
    
        //output the content to the file.
        File outFile = new File(xmlFileName);
    
        outFile.getParentFile().mkdirs();
    
        FileWriter writer = new FileWriter(outFile);
    
        writer.write(out.toString(charsetName));
    
        writer.flush();
        writer.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    return IApplication.EXIT_OK;
    

    }

To create a standalone application, you also need to create a Product Configuration.

  • Right-click->New->Other...->Plug-in Development->Product Configuration
  • Select the RCP plugin project
  • Enter config in the File Name: field and click Finish.
  • Enter some suitable values in the config.product editor ID, version and Name fields (they don't really matter as it is a headless product).
  • In Product Definition section, select New... button next to the Product: field, the default values should be fine (double check the Defining Plug-in is your RCP plug-in), select OK
  • Save the product

You now need to export the RCP application.

  • Right click on the project->Export...->Plug-in Development->Eclipse product
  • Enter a destination directory for the application, and select OK

You should now have a standalone application that you can invoke as you would any other application, passing command-line parameters to generate an XML file from a schema.

The expected parameters are, in order:

  • Fully-qualified path to the schema to generate from
  • Fully-qualified name of the file to create (parent directories will also be created).
  • The root element name (same as in the wizard, this is the name of the element you want to generate content below).

NOTE This process will only generate a file from a single schema, if your schema references other schemas it will currently fail. It would be possible to extend the process to take a properties file listing all the referenced schema locations, and resolve those as catalog contributions so the process can resolve the schemas. Some notes on how and why you'd do this below.
If I get the chance I'll look into implementing this and update my answer accordingly.


If you have, for example, a Spring schema you may want to include the various Spring namespace schema in your schema file. In Eclipse the Catalog contributions provide a means to map those schema IDs to the schema file location so it can be parsed. If you have plugins for them, they could be bundled with the application and define catalog contributions (see the help for pointers on contributing them).

If you don't have catalog contributions available, the process would instead define key-value pairs in the properties file to reference the schema locations on the drive.

Example contents:

 http://www.springframework.org/schema/beans=c:\\schema\\spring-beans.xsd
 http://www.springframework.org/schema/tool=c:\\schema\\spring-tool.xsd
南风几经秋 2024-08-12 18:13:16

您看到这个问题了吗?它并不是专门指无头环境,但非常相似。

建议之一 是 Sun XMLGenerator 工具。我尝试了一下,效果非常好。

Did you see this question? It doesn't refer specifically to headless environments, but it's pretty similar.

One of the suggestions was the Sun XMLGenerator tool. I tried it and it worked like a charm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文