Boost属性ptree:提升write_xml在xml文件的子元素中添加unicode 0x0字符

时间:2017-11-02 09:21:23

标签: c++ xml boost unicode boost-propertytree

我正在使用boost >>> import random >>> l = [(random.random(), random.random()) for _ in xrange(1000)] >>> %timeit [(low, high) for (low, high) in l if high <= x <= low] The slowest run took 5.41 times longer than the fastest. This could mean that an intermediate result is being cached. 10000 loops, best of 3: 82 µs per loop >>> a = np.array(l) >>> %timeit a[(a[...,0] < x) & (a[...,1] > x)] The slowest run took 6.01 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 10.6 µs per loop >>> 函数来创建xml。我可以使用Boost创建成功的xml。但它在xml子元素的末尾添加了额外的unicode 0x0字符。

代码段:

write_xml

我正在将这个xml发送到Java Side应用程序,并且在解析boost创建的xml时抛出Java异常错误。

An Invalid XML character(Unicode: 0x0) was found in the element content of the document error

任何人都知道,如何在使用boost::property_tree::write_xml(oss, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4)); 创建xml时从XML中删除unicode 0x0 character

1 个答案:

答案 0 :(得分:0)

您的数据嵌入了NUL字节。实现这一目标的一种方法:

std::string const hazard("erm\0", 4); 
boost::property_tree::ptree pt;
pt.put("a.b.c.<xmlattr>.d", hazard);

更新

经过仔细检查,NUL字节只是XML中的 不支持 ,完全停止(Storing the value Null (ASCII) in XML)。

要么摆脱有问题的字节,要么使用某种编码,比如base64。

旧的分析和演示如下

请注意,Property Tree不是XML库,因此可能存在不符合XML标准的限制。

我仍然认为这是一个BUG,因为它没有往返:Property Tree无法读回自己的序列化属性树:

<强> Live On Coliru

#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

int main() {
    std::string const hazard("erm\0", 4); 

    {
        std::ofstream ofs("NULbyte.xml");

        boost::property_tree::ptree pt;
        pt.put("a.b.c.<xmlattr>.d", hazard);

        write_xml(ofs, pt);
    }
    {
        std::ifstream ifs("NULbyte.xml");

        boost::property_tree::ptree pt;
        read_xml(ifs, pt);
        std::cout << (hazard == pt.get<std::string>("a.b.c.<xmlattr>.d")) << "\n";
    }
}

如果需要,您可以正确使用JSON后端: Live On Coliru