使用 eclipse 从 xsd 生成无头 xml
在最新版本的免费开源 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个无头 RCP 应用程序,其中仅包含执行以下操作所需的插件实际一代。这些主要是 WTP 插件,其中包含一些管理扩展点等所需的核心插件。
RCP 应用程序可以从命令行运行,并传递要生成的架构的参数和输出文件名。它缺少您在生产实现中可能需要的大部分验证,但向您展示了如何完成它。
它还将字符集硬编码为 UTF-8,您可以扩展参数处理以使其成为可选参数或其他参数。
下面的代码片段可以合并到新的无头 RCP 应用程序中。要创建RCP应用程序,首先创建一个新的Plugin项目:
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();
}
创建独立应用程序,您还需要创建产品配置。
您现在需要导出 RCP 应用程序。
您现在应该有一个独立的应用程序您可以像调用任何其他应用程序一样调用,传递命令行参数以从架构生成 XML 文件。
预期参数按顺序为:
注意此过程只会从单个架构生成文件,如果您的架构引用其他架构,当前将会失败。可以扩展该流程以获取列出所有引用模式位置的属性文件,并将这些文件解析为目录贡献,以便该流程可以解析模式。下面是关于如何以及为何执行此操作的一些说明。
如果有机会,我会考虑实施这一点并相应地更新我的答案。
例如,如果您有一个 Spring 模式,您可能希望在模式文件中包含各种 Spring 命名空间模式。在 Eclipse 中,目录贡献提供了一种将这些模式 ID 映射到模式文件位置的方法,以便可以对其进行解析。如果您有它们的插件,它们可以与应用程序捆绑在一起并定义目录贡献(请参阅 帮助 有关贡献它们的指示)。
如果您没有可用的目录贡献,该过程将改为在属性文件中定义键值对以引用驱动器上的架构位置。
示例内容:
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:
In the project you should see an
Application
class, copy the Java content below into thestart()
method of theApplication
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();
}
To create a standalone application, you also need to create a Product Configuration.
You now need to export the RCP application.
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:
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:
您看到这个问题了吗?它并不是专门指无头环境,但非常相似。
建议之一 是 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.