将配置文件转换为 XML
我们有大量的遗留配置文件,格式各异,通常类似于关键字数据关键字数据关键字数据。
数据本身的格式在每个配置文件中都是唯一的。
我们想要做的是以某种方式定义文件数据格式,然后使用它来允许应用程序根据定义的文件格式检查配置文件。
我们曾考虑过将它们定义为 BNF 并使用 YACC 或其等效物,但令人烦恼的是,必须有一种方法可以使用 XML 来做到这一点。
所需要的是一种以 XML 格式定义配置文件数据格式的方法,然后使用该文件将遗留文件转换为有效的 XML。 将 XML 文件转换回旧文件格式的首选方法会很有用。
We have a large number of legacy configuration files, of various formats normally something like KEYWORD DATA KEYWORD DATA KEYWORD DATA.
The the format of the data itself is unique within each configuration file.
What we would like to do is define the file data formats in some way and then use that to allow a application to check a the configuration files against to defined file formats.
We have thought about defining them as BNF and using YACC or its equivalent, but the nagging feeling is that there must be a away of doing this using XML.
What would be required was a way of defining a configuration files data format preferable in a XML format, then use that file to convert the legacy file into valid XML. Preferable a way of converting the XML file back to the legacy file format would be useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于转换 XML->legacy 文件,XSLT 可能会工作得很好。
For the convertion XML->legacy file, XSLT would probably work fine.
这正是 Gelatin 设计的问题类型。 (另外,自我推销警告。)
This is precisely the type of problem that Gelatin was designed for. (Also, self promotion warning.)
查看 Altova-Tools,尤其是 Mapforce。 AFAIR 他们可以在用户文件格式之间进行转换,并且映射可以在屏幕上非常自然地完成。 (Altova Tools 还可以生成 XSD 进行检查。)
Have a look at the Altova-Tools, especially Mapforce. AFAIR they can convert from/to user-file-format and the mapping can be done quite naturally on screen. (Altova Tools also can generate XSD to check against.)
尝试使用 awk(或 gawk)等简单的文本处理器来生成 XML。 图案看起来像这样。
确保编码正确。 对于英文配置文件,“ASCII”就足够了。
之后,您可以使用各种工具来处理该 XML。 我建议使用这种格式,因为它的创建和处理最简单:
为您拥有的每种类型的配置文件使用不同的“配置类型”名称,以便轻松区分它们。
要检查 XML 的格式,最简单的方法是为其定义 DTD。 许多 XML 编辑器可以读取现有的 XML 文件并为其创建 DTD。 该 DTD 并不完美,但它将是一个非常好的起点。
然后,您可以在 XML 标头中指定 DTD 的名称,并告诉 XML 解析器验证结构(但不是数据)。
要检查数据,您可以使用 XML 模式,但 XML 模式非常复杂,而且常常是一种矫枉过正的情况。
Try to use a simple text processor like awk (or gawk) to generate the XML. The pattern would look like this.
Make sure the encoding is correct. For config files in English, "ASCII" is enough.
After that, you can use a wide variety of tools to process that XML. I suggest to use this format because it's most simple to create and process:
Use a different name for "config-type" for each type of config file you have so they are easy to distinguish.
To check the format of the XML, the most simple way is to define a DTD for it. Many XML editors can read an existing XML file and create a DTD for it. That DTD won't be perfect but it will be a very good starting point.
You can then specify the name of the DTD in the XML header and tell the XML parser to validate the structure (not the data, though).
To check the data, you can use XML schema but XML schema is very complicated and often an overkill.