c++ - 为什么 boost::property_tree::read_xml 抛出读取有效的 Office Open XML?

标签 c++ xml excel exception boost

我正在使用 Boost 1.62.0 并尝试根据 the ISO standard 解析一个 Excel .xlsx 文件包含各种 XML 文件。它读取 [Content_Types].xml 文件就好了。但是对于我试图读取的第二个 (workbook.xml.rels) 和第三个 (workbook.xml) 文件,它会引发如下异常:

/usr/local/include/boost/property_tree/detail/xml_parser_read_rapidxml.hpp(138): Throw in function void boost::property_tree::xml_parser::read_xml_internal(std::basic_istream<typename Ptree::key_type::value_type> &, Ptree &, int, const std::string &) [Ptree = boost::property_tree::basic_ptree<std::__1::basic_string<char>, std::__1::basic_string<char>, std::__1::less<std::__1::basic_string<char> > >]
Dynamic exception type: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::xml_parser::xml_parser_error> >
std::exception::what: ../tmp/UX0s9R9kBpSxFB40s9OTzTD9/xl/_rels/workbook.xml.rels(1): expected <

这里是/xl/_rels/workbook.xml.rels(默认缩小):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet8.xml"/><Relationship Id="rId13" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet13.xml"/><Relationship Id="rId18" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet3.xml"/><Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet7.xml"/><Relationship Id="rId12" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet12.xml"/><Relationship Id="rId17" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet17.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml"/><Relationship Id="rId16" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet16.xml"/><Relationship Id="rId20" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet6.xml"/><Relationship Id="rId11" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet11.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet5.xml"/><Relationship Id="rId15" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet15.xml"/><Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet10.xml"/><Relationship Id="rId19" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet4.xml"/><Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet9.xml"/><Relationship Id="rId14" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet14.xml"/></Relationships>

和/xl/workbook.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="9302"/><workbookPr defaultThemeVersion="124226"/><bookViews><workbookView xWindow="360" yWindow="420" windowWidth="24675" windowHeight="11805" firstSheet="3" activeTab="16"/></bookViews><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1"/><sheet name="Sheet2" sheetId="2" r:id="rId2"/><sheet name="Sheet3" sheetId="3" r:id="rId3"/><sheet name="Sheet4" sheetId="4" r:id="rId4"/><sheet name="Sheet5" sheetId="5" r:id="rId5"/><sheet name="Sheet6" sheetId="6" r:id="rId6"/><sheet name="Sheet7" sheetId="7" r:id="rId7"/><sheet name="Sheet8" sheetId="8" r:id="rId8"/><sheet name="Sheet9" sheetId="9" r:id="rId9"/><sheet name="Sheet10" sheetId="10" r:id="rId10"/><sheet name="Sheet11" sheetId="11" r:id="rId11"/><sheet name="Sheet12" sheetId="12" r:id="rId12"/><sheet name="Sheet13" sheetId="13" r:id="rId13"/><sheet name="Sheet14" sheetId="14" r:id="rId14"/><sheet name="Sheet15" sheetId="15" r:id="rId15"/><sheet name="Sheet16" sheetId="16" r:id="rId16"/><sheet name="Sheet17" sheetId="17" r:id="rId17"/></sheets><calcPr calcId="144525"/></workbook>

我已经使用各种在线 XML 验证器验证了 XML,并且它通过了所有验证器。知道为什么 boost::property_tree::read_xml 在第一行需要一个 '<' 吗?

最佳答案

好的,我从boost documentation找到了答案:

Unfortunately, there is no XML parser in Boost as of the time of this writing. The library therefore contains the fast and tiny RapidXML parser (currently in version 1.13) to provide XML parsing support. RapidXML does not fully support the XML standard; it is not capable of parsing DTDs and therefore cannot do full entity substitution.

所以,看来我必须使用另一个 XML 解析器。

关于c++ - 为什么 boost::property_tree::read_xml 抛出读取有效的 Office Open XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41516761/

相关文章:

java - 网络服务 : How to store mal-formed XML in webservices?

Excel VBA (UDF) 返回#VALUE!由于 255 个字符的限制

python - 使用 pandas 将 xlsx 文件中的特定行添加到列表

c++ - 向库用户隐藏库依赖项

sql-server - 在 T-SQL 中查询之前将字符串转换为 XML 数据类型

android - 如何从服务器获取数据并用它更新数据库?

android - 从 Assets 文件夹读取 Excel 工作簿时出现问题

c++ - 在 Linux 上使用 CMake 时,如何告诉 PVS-Studio 忽略第三方库中的所有文件

c++ - C中1的稀疏矩阵的快速矩阵乘法

c++ - 为什么 istream_iterator<unsigned char, unsigned char> 抛出 std::bad_cast?