C++ 通过SQLServerXML数据类型在xml和xml之间增强序列化异常
好的,这是我的问题 我有一个boost对象,它通过序列化创建xml字符串,这很好用 使用boost 1.38版 我将该xml字符串保存到sql server数据库表中的xml数据类型中,这也可以正常工作 然后我从数据库表中检索xml字符串,但格式与插入时略有不同,基本上空白值被短标记,从到 下面是一个前后xml的示例 以前 测试表 2. 0 AAAA xxxx BBBB 之后 测试表 2. 0 AAAA xxxx BBBB 这也很好,完全可以接受,并非意外 当我使用这个xml字符串并尝试将xml序列化回boost对象时,问题就出现了,当遇到xml字符串中的短标记时,它会抛出异常 我遇到了麻烦,不知道如何解决这个问题,在网上也找不到任何关于这个问题的参考资料,所以如果有任何帮助,我将不胜感激 : 这是我的代码,它应该编译没有任何问题,您只需要填写db部分的空白: grid.hppC++ 通过SQLServerXML数据类型在xml和xml之间增强序列化异常,c++,serialization,boost,xml-serialization,boost-serialization,C++,Serialization,Boost,Xml Serialization,Boost Serialization,好的,这是我的问题 我有一个boost对象,它通过序列化创建xml字符串,这很好用 使用boost 1.38版 我将该xml字符串保存到sql server数据库表中的xml数据类型中,这也可以正常工作 然后我从数据库表中检索xml字符串,但格式与插入时略有不同,基本上空白值被短标记,从到 下面是一个前后xml的示例 以前 测试表 2. 0 AAAA xxxx BBBB 之后 测试表 2. 0 AAAA xxxx BBBB 这也很好,完全可以接受,并非意外 当我使用这个xml字符串并尝试将xml
////////////////////////////////////////////////////////////////
// grid boost serialization object
//
#pragma once
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/version.hpp>
/////////////////////////////////////////////////////////////
// Column
//
namespace sdcm
{
class Column
{
public:
// every serializable class needs a constructor
Column()
{
}
Column(const std::string& _label, const std::string& _data)
: label(_label),
data(_data)
{
}
private:
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const Column &col);
std::string label;
std::string data;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */)
{
ar & BOOST_SERIALIZATION_NVP(label)
& BOOST_SERIALIZATION_NVP(data)
;
}
};
class Grid
{
public:
// every serializable class needs a constructor
Grid()
{
}
Grid(const std::string& _name)
: name(_name)
{
}
void append(Column* col)
{
columns.insert(columns.end(), col);
}
private:
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const Grid &grid);
std::string name;
typedef Column* GRID_COLUMNS;
std::list<GRID_COLUMNS> columns;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(name)
& BOOST_SERIALIZATION_NVP(columns);
}
};
} // end namespace
main.cpp
// boost_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <sstream>
#include <iostream>
#include <string>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include "grid.hpp"
std::string get_grid_as_xml_str(sdcm::Grid& grid)
{
std::ostringstream xml ;
unsigned int flags = boost::archive::no_header
//| boost::archive::no_codecvt
//| boost::archive::no_xml_tag_checking
//| boost::archive::no_tracking
;
boost::archive::xml_oarchive oa(xml, flags);
oa << BOOST_SERIALIZATION_NVP(grid);
return xml.str();
}
void restore_grid_from_xml_str(sdcm::Grid& grid, const std::string& xml_str)
{
std::istringstream xml(xml_str);
unsigned int flags = boost::archive::no_header
//| boost::archive::no_codecvt
//| boost::archive::no_xml_tag_checking
//| boost::archive::no_tracking
;
boost::archive::xml_iarchive ia(xml, flags);
ia >> BOOST_SERIALIZATION_NVP(grid);
}
int _tmain(int argc, _TCHAR* argv[])
{
// create grid obj with cols
sdcm::Grid grid("MY GRID");
grid.append(new sdcm::Column("AAAA", "xxxx")) ;
grid.append(new sdcm::Column("BBBB", "")) ;
// get grid as xml string
std::string xml = get_grid_as_xml_str(grid) ;
// Assume xml is saved to SQL Server DB table in XML datatype,
// and the data has be retrived is a shorted tag format used
// where blank values are present in tags
// make a new grid
sdcm::Grid new_grid;
restore_grid_from_xml_str(new_grid, xml);
return 0;
}
有点晚了,但我确实收到了boost的回复邮件 是的,这是一个bug,但他们不会修复它 如果我们感兴趣,请回复: xml_归档代码以及所有序列化代码都假定 我们加载的正是我们保存的内容。总的来说,我想让它更 将军似乎不值得冒险。如果您想创建、测试和 向spirit解析器提交一个补丁,该解析器将处理标记 我会看的。但是如果没有这些,我不想花时间 在这个问题上
有点晚了,但我确实收到了boost的回复邮件 是的,这是一个bug,但他们不会修复它 如果我们感兴趣,请回复: xml_归档代码以及所有序列化代码都假定 我们加载的正是我们保存的内容。总的来说,我想让它更 将军似乎不值得冒险。如果您想创建、测试和 向spirit解析器提交一个补丁,该解析器将处理标记 我会看的。但是如果没有这些,我不想花时间 在这个问题上
boost单元测试有什么帮助吗?看看所有的文档、示例和测试,它们并没有为我的问题提供任何提示。我已经深入研究了boost序列化代码,从表面上看,“短标记”似乎没有得到处理,这有点令人惊讶,因此我将提交一份错误报告,看看他们说了什么。boost单元测试有什么帮助吗?查看所有文档、示例和测试,他们对我的问题没有任何暗示。我已经深入研究了boost序列化代码,从表面上看,“短标记”似乎没有得到处理,这有点令人惊讶,所以我将提交一份bug报告,看看他们怎么说。