我遇到了一个问题,我将一个对象序列化为一个字符串,然后从同一个字符串再次创建它。当我尝试从字符串创建相同的对象并检查值时,函数没有正确返回。它将整个 XML 对象作为字符串返回。
我查看了生成的 xml,它看起来是正确的。所以我不知道该怎么做。
感谢您对此问题的任何见解。
下面是一个例子。
#include "XMLHelper.hxx"
void serializeObject(XmlHelper::Object& mess, std::string& result);
int _tmain(int argc, _TCHAR* argv[])
{
XmlHelper::objectType oType(XmlHelper::objectType::Status);
XmlHelper::Object obj(oType);
unsigned long time(134000000);
XmlHelper::Status status(time);
obj.status().set(status);
std::string result;
serializeObject(obj, result);
if (obj.status().present() )
{
std::cout<<"Message is Status"<<std::endl;
}
if (obj.objectType() == "Status" )
{
std::cout<<"Message is Status"<<std::endl;
}
if (obj.objectType() == XmlHelper::objectType::Status )
{
std::cout<<"Message is Status"<<std::endl;
}
XmlHelper::Object otherObj(result);
if (otherObj.status().present() )
{
std::cout<<"Message is Status"<<std::endl;
}
else if (otherObj.objectType() == "ThingA")
{
std::cout<<"Message is ThingA"<<std::endl;
}
else if (otherObj.objectType() == "ThingB")
{
std::cout<<"Message is ThingB"<<std::endl;
}
return 0;
}
void serializeObject(XmlHelper::Object& mess, std::string& result)
{
std::ostringstream buff;
xml_schema::namespace_infomap nsm;
nsm[""].schema = "XMLHelper.xsd";
Object_(buff, mess, nsm, "UTF-8", xml_schema::flags::no_xml_declaration);
result=buff.str();
}
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<!--<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/TENAXML">-->
<xsd:complexType name ="Status" >
<xsd:sequence>
<xsd:element name="timeOfUpdate" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name ="ThingA" >
<xsd:sequence>
<xsd:element name="number" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name ="ThingB" >
<xsd:sequence>
<xsd:element name="number" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="objectType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Status"/>
<xsd:enumeration value="ThingA"/>
<xsd:enumeration value="ThingB"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="Object" >
<xsd:sequence>
<xsd:element name="objectType" type ="objectType" minOccurs="1" maxOccurs="1" />
<xsd:element name ="thingA" type ="ThingA" minOccurs="0" maxOccurs="1"/>
<xsd:element name ="thingB" type ="ThingB" minOccurs="0" maxOccurs="1"/>
<xsd:element name ="status" type ="Status" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType >
<xsd:element name="Object" type="Object" />
</xsd:schema>