【问题标题】:Poco::DOMParser and wstringPoco::DOMParser 和 wstring
【发布时间】:2014-06-03 06:19:17
【问题描述】:

有没有办法让Poco::DOMParserstd::wstring 一起工作?

我使用定义的 XML_UNICODEXML_UNICODE_WCHAR_T 构建 pocoXML 库。现在 我正在尝试编译这样的代码:

Poco::XML::DOMParser parser;
std::wstring xml = getXml(); //Init string with xml document

// Cannot compile, cause parseString wants only std::string
// not wstring
Poco::AutoPtr<Poco::XML::Document> document = parser.parseString(xml); 

Poco::XML::SaxParser 可以解析 std::wstringPoco::XML::DOMParser 使用 SAXParser 构建文档。所以我不知道为什么我不能将 std::wstring 传递给 parseString。

也许我不能使用 parseString,但还有另一种方法可以用 DOMParser 解析 std::wstring 吗?

【问题讨论】:

    标签: c++ xml xml-parsing poco-libraries wstring


    【解决方案1】:

    您应该能够在仅定义XML_UNICODE_WCHAR_T 的Windows 上使用std::wstring。在 POSIX 平台上,您必须确保 wchar_t 为 2 个字节宽(检查 __SIZEOF_WCHAR_T__ 定义)。根据XML_UNICODE_WCHAR_T 定义,使用从std::basic_string 适当定义的XMLString 是一种很好的做法。

    但是,有一个问题阻止了使用 XML_UNICODE_WCHAR_T 进行 Poco::XML 编译。 Github issue 已经创建,问题现在是 fixed1.5.3 release 的开发分支中。

    【讨论】:

    • 我和我的同事通过使用 toXMLString 和 fromXMLString 来转换 std::string 解决了这个问题。可能这是不正确的。 (parseString 方法仍然不可用)。我猜没有人将 DOMParser 用于 wstring,所以这个功能不可用。 PocoUtils/XMLconfiguration.cpp 我们也遇到了同样的问题(不能用 XML_UNICODE 和 XML_UNICODE_WCHAR_T 编译)
    • 只要确保 std::string 是 UTF-8 编码的,就可以在 std::string 之间来回转换。当然,您会为转换付出一些性能损失。Util 依赖于 XML,因此如果 XML 构建被 XML_UNICODE_WCHAR_T 破坏,Util 也不会构建。上面提到的修复应该解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2014-10-22
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2011-02-17
    • 2014-10-06
    相关资源
    最近更新 更多