问题描述
在最新版本的Eclipse IDE中,您可以从DTD和XSD文件生成XML文档。右键单击给定的* .dtd或* .xsd文件,然后选择生成 - > XML文件...。您可以选择要生成的根元素,以及是否应生成可选属性和元素。
我可以使用这个无头(不启动eclipse)吗?
您可以创建一个无头点的应用程序,其中只包含需要做的插件实际一代。这些主要是具有管理扩展点等所需的几个核心插件的WTP插件。
RCP应用程序可以从命令行运行,并传递参数要生成的模式和输出文件名。它在生产实现中缺少许多验证,但可以显示如何完成。
它还可以将字符集转换为UTF-8,您可以扩展参数处理以使其成为可选参数。
下面的代码片段可以被合并成为新的无头RCP应用程序。要创建RCP应用程序,首先创建一个新的Plugin项目:
- 右键单击 - > New-> Other ...-> Plug在开发 - >插件项目中,选择下一步
- 输入项目的名称(例如 name.seller.rich.xmlgen ),然后选择下一步
- 取消选中此插件将为用户界面做出贡献,然后选择是 Rich Client应用程序,然后单击完成
- 要添加所需的依赖关系,请双击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 。核心; bundl e-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
-
在项目中, code>应用程序 class,将下面的Java内容复制到
应用程序的
start()
code> source(并导入文件顶部)。
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]; // eg C:\test\test.xsd
String xmlFileName = args [1]; //例如C:\test\test.xml
String rootName = args [2]; //myTestRoot;
String charsetName =UTF-8;
try {
//获取URI作为完整的URL路径
URI schemaUri = new File(schemaFileName).toURI();
schemaFileName = schemaUri.toURL()。toString();
// TODO处理任何错误信息设置到这个数组
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);
//将内容输出到文件。
文件outFile =新文件(xmlFileName);
outFile.getParentFile()。mkdirs();
FileWriter writer = new FileWriter(outFile);
writer.write(out.toString(charsetName));
writer.flush();
writer.close();
} catch(Exception e){
// TODO自动生成的catch块
e.printStackTrace();
}
返回IApplication.EXIT_OK;
}
要创建独立应用程序,还需要创建一个产品配置。
- 右键单击 - >新建 - >其他... - >插件开发 - >产品配置
- 选择RCP插件项目
- 在文件名:字段中输入配置,然后单击完成。
- 在config.product编辑器ID,版本和名称字段中输入一些合适的值
- 在产品定义部分中,选择旁边的新建... 按钮产品:字段,默认值应该很好(双击确定插件是您的RCP插件),选择确定
- 保存产品
您现在需要导出RCP应用程序。
- 右键单击项目 - >导出...->插件开发 - > Eclipse产品
- 输入应用程序的目的地目录,然后选择确定
您现在应该有您可以像任何其他应用程序一样调用独立应用程序,传递命令行参数以从模式生成XML文件。
预期参数按顺序:
- 要从
- 生成模式的完全限定路径文件的全限定名称创建(也将创建父目录)。
- 根元素名称(与向导中相同,这是要在下面生成内容的元素的名称)。 li>
注意此过程只会从单个生成一个文件模式,如果您的模式引用其他模式,它将当前失败。可以扩展进程以获取列出所有引用的模式位置的属性文件,并将其解析为目录贡献,以便进程可以解析模式。一些关于如何和为什么你会这样做的注释。
如果我有机会,我会考虑实施这个并相应地更新我的答案。
例如,如果您有一个Spring模式,您可能希望在模式文件中包含各种Spring命名空间模式。在Eclipse中,目录贡献提供了一种将这些模式标识映射到模式文件位置的方法,以便可以进行解析。如果您有插件,则可以将它们与应用程序捆绑在一起,并定义目录贡献(请参阅,以指导他们的贡献。
如果您没有可用的目录供稿,则该过程将在属性文件中定义键值对,以引用驱动器上的模式位置。 >
示例内容:
http://www.springframework.org/schema或者/ tool.xsd
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)?
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 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();
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
这篇关于从xsd与eclipse无头xml生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!