Question

I'm having an issue where I serialize an object to a string and then create it again from the same string. When I try and create the same object from the string and check the values, the functions aren't returning correctly. It returns the whole XML object as a string.

I looked at the resulting xml and it looks correct. So I'm at a loss as to what to do.

Thanks for any insight into this issue.

An example is below.

#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>
Was it helpful?

Solution

Come to find out I was doing the building of the object which parses the XML completely wrong. What seems to be the correct way is the below.

std::istringstream iss (result); 
std::auto_ptr<XmlHelper::Object> otherObj(XmlHelper::Object_(iss)); 

I also didn't have my xsd file in the correct directory.

Hopefully, this helps someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top