解析包含多个 xml 的文件 [英] Parsing a file with multiple xmls in it

查看:32
本文介绍了解析包含多个 xml 的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法解析包含多个 xml 的文件?

Is there a way to parse a file which contains multiple xmls in it?

例如,如果我有一个名为stocks.xml 的文件并且在stocks.xml 中我有多个xml 内容,有没有办法解析这个xml 文件?.

eg., if I have a file called stocks.xml and within the stocks.xml i have more than one xml content, is there any way to parse this xml file ?.

--stocks.xml

-- stocks.xml

<?xml version="1.0" encoding="ASCII"?><PRODUCT><ID>A001</ID>..</PRODUCT><SHOP-1><QUANTITY>nn</QUANITY><SHOP-1><QUANTITY>nn</QUANITY>
<?xml version="1.0" encoding="ASCII"?><PRODUCT><ID>A002</ID>..</PRODUCT><SHOP-1><QUANTITY>nn</QUANITY><SHOP-1><QUANTITY>nn</QUANITY>

推荐答案

如果您可以假设每个 xml 文档都以 <?xml version="1.0" ... 开头,只需阅读逐行查找与该模式匹配的行(或者,读取所有数据,然后搜索数据).

If you can assume that each xml document begins with <?xml version="1.0" ..., simply read the file line-by-line looking for a lines that match that pattern (or, read all the data and then do a search through the data).

一旦找到一行,就保留它,并追加后续行,直到找到下一个 xml 文档或点击 EOF.起泡,冲洗,重复.

Once you find a line, keep it, and append subsequent lines until the next xml document is found or you hit EOF. lather, rinse, repeat.

您现在有一个字符串形式的 xml 文档.然后,您可以使用普通的 XML 解析工具解析字符串,或者将其写入文件.

You now have one xml document in a string. You can then parse the string using the normal XML parsing tools, or you write it to a file.

这在大多数情况下都可以正常工作,但是如果您的一个嵌入的 xml 文档包含与文档开头完全匹配的数据,它当然可能会失败.您很可能不必担心这一点,如果您这样做了,可以通过更聪明的方式避免这种情况.

This will work fine in most cases, but of course it could fall down if one of your embedded xml documents contains data that exactly matches the same pattern as the beginning of a document. Most likely you don't have to worry about that, and if you do there are ways to avoid that with a little more cleverness.

正确的解决方案实际上取决于您的需求.如果您正在创建一个通用的必须始终工作的解决方案,这可能不适合您.对于现实世界中的特殊用途问题,它可能不仅仅是足够好",而且通常足够好"确实是足够好".

The right solution really depends on your needs. If you're creating a general purpose must-work-at-all-times solution this might not be right for you. For real world, special purpose problems it's probably more than Good Enough, and often Good Enough is indeed Good Enough.

这篇关于解析包含多个 xml 的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆