使用批处理文件将多个 xml 文档合并为一个大文档
我有一个包含 86 个 xml 文件的目录,这些文件具有相同的列和格式,我需要将它们组合成一个大的 xml 文件。我对批处理文件非常缺乏经验,我的第一次尝试是简单地将一个文件文本附加到下一个使用...
FOR %%i IN (directory\*.001) DO type %%i >> directory\combo_file.001
不幸的是,当我尝试在 Excel 中打开它时,这会产生解析错误。我想这是因为许多字段和标签是重复的。有谁知道我怎样才能实现这一目标?我只需要在 Excel 中打开此文件,因此如果可以的话,我愿意将文件转换为 CSV。
非常感谢任何帮助,谢谢!
I have a directory of 86 xml files with the same columns and formatting that I need to combine into one large xml file. I am very inexperienced with batch files, and my first attempt was to simply append one file text onto the next using...
FOR %%i IN (directory\*.001) DO type %%i >> directory\combo_file.001
Unfortunately, this creates a Parse error when i try to open it in excel. I would imagine that this is because many fields and tags are repeated. Does anyone know how I might be able to achieve this? I only need to open this file in excel, so I would be open to converting files to CSV, if that was an option.
Any help is much appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一个非常简单的方法是进行简单的复制:
创建的 new.xml 文件会将所有 xml 文件合并在一起。您可以使用相同的命令创建 BAT 文件
A very easy approach will be to do a simple copy:
The new.xml file created will have all the xml files merged together. You can create a BAT file with the same command
下面是一个快速批处理命令,它将当前目录中的所有 xml 文件组合到一个文件中:CombineXML.bat。它使用新的根节点(“”)包装所有 XML 文件。
然而,就您的情况而言,您可能不想向 XML 引入这个新层。
如果您所做的只是在单个区域(例如:在 Web 浏览器中)查看 XML,则此方法有效。
Here's a quick batch command that combines all the xml files in the current directory into a single file CombineXML.bat. It wraps all the XML files with a new root node ("<root>").
In your case, however, you may not want to introduce this new layer to the XML.
If all you're doing is viewing the XML in a single area (eg: in a web browser) then this works.
问题在于 XML 有一个起始标记,例如:
,该标记将在合并的文档中重复。此外,如果有一个根标签包含所有其他标签,则合并时会多次包含该根标签。
我认为,即使使用强大的 shell 和 Linux/Unix 中的命令(find、grep 等),在批处理 shell 中执行此操作也非常困难(如果可能的话)。
我会使用一个简单的程序(例如,VBA)来做到这一点。
编辑:我发现在 Excel 中您可以导入多个 xml 文件。您必须转到“开发”选项卡(如果隐藏则显示它)。然后在 XML 组中,选择导入并选择多个 XML 文件。那应该有效。
The problem is that XML has a single starting tag like:
<?xml version="1.0" encoding="UTF-8"?>
that would be repeated on the merged document. Also, if there's a root tag that contains all the others, merging you would include the root tag multiple times.I think that it would be very hard (if possible) to do that in a batch shell, even using a powerful shell and commands like those in Linux/Unix (find, grep, etc).
I would use a simple program (say, VBA) to do that.
edit: I've found that in Excel you can import multiple xml files. You have to go to the Develop tab (show it if it's hidden). Then in the XML group, choose Import and select multiple XML files. That should work.
当与 ANT 一起使用时,任务就足够了:
此代码生成一个通用 XML 元素,并在其中收集根据文件集找到的任何 .xml 文件的内容。请参阅:
When used with ANT, the <concat> task would be enough:
This code produces a common XML element and collects in it the contents of any .xml files it finds according to the file set. See:
当您使用 xslt 处理这一文件时,它看起来像是所有文件的组合。
When you process this one file with xslt it looks like all of the files combined.
我从上面的批处理命令中获取了示例,将一个目录中的 100 多个内容合并到一个 csv 中。并且效果很好。
And I have taken the sample from the above batch command to combine 100+ from one directory into one csv. and works very well.