python - 具有多个 DOCTYPE 声明的 XML
问题描述
您好,我有一个相当大的 XML 文件 10-15gb。它包含多个根 Doctype 标记,我猜是谁制作它只是将一堆单独的文件连接在一起。这绝对不是最佳实践,但有时它就是您必须使用的全部。我想知道是否有人有解析文件或将文件分成每个单独的 DocType 的解决方案。
到目前为止,我已经尝试将整个文件包装在一个根标签中,但这不起作用。我正在使用 Python。
任何解决方案或输入将不胜感激。
<?xml version="1.0" ?>
<!DOCTYPE pmc-articleset PUBLIC "-//NLM//DTD ARTICLE SET 2.0//EN" "https://dtd.nlm.nih.gov/ncbi/pmc/articleset/nlm-articleset-2.0.dtd">
<pmc-articleset><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article">
<?properties open_access?>
<front>
<p>
Apple
</p>
</front>
</article>
</pmc-articleset>
<?xml version="1.0" ?>
<!DOCTYPE pmc-articleset PUBLIC "-//NLM//DTD ARTICLE SET 2.0//EN" "https://dtd.nlm.nih.gov/ncbi/pmc/articleset/nlm-articleset-2.0.dtd">
<pmc-articleset><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article">
<?properties open_access?>
<front>
<p>
Banana
</p>
</front>
</article>
</pmc-articleset>
解决方案
可以使用 将文件拆分为多个部分csplit(1)
,这是该任务的实用程序。
在 XML 声明中<?xml ...
csplit -z --prefix output_file --suffix-format '%02d.xml' your_large.xml '/^<[?]xml[ ]/' {*}
或者,如果不重复,在<!DOCTYPE
csplit -z --prefix output_file --suffix-format '%02d.xml' your_large.xml '/<!DOCTYPE/' {*}
这将导致output_file00.xml
,output_file01.xml
等。
推荐阅读
- bash - Bash printf:使用 Ctrl+U 清除整行
- python-3.x - 尝试使用 pip 为机器人框架安装 AppiumLibrary 时出现语法错误
- android - 清单合并失败:uses-sdk:minSdkVersion 19 不能小于库中声明的版本 22
- python - 行不均匀的刮板刮板
- facebook - FB 营销 API 更新广告素材时出错
- java - 通过构造函数进行 Bean 实例化失败
- azure - 多个订阅者将其转发到同一个队列 - Azure
- matlab - 如何将文件名附加到数组 - Matlab
- reactjs - 为什么在 React.JS 中没有获取本地 JSON 文件?
- macos - 如何在 Mac 终端上使用私钥和 ssh 登录?我的尝试导致`Load key "/path/to/privatekey": invalid format`