シリアル化されたオブジェクトからオブジェクトを作成することにXerces Cの問題

StackOverflow https://stackoverflow.com//questions/10669994

質問

オブジェクトを文字列にシリアル化してから同じ文字列から再度作成するという問題があります。文字列から同じオブジェクトを作成して値を確認すると、関数は正しく戻っていません。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>
.

役に立ちましたか?

解決

私がXMLを完全に間違っているオブジェクトの建物をしていたことを見つけてください。正しい方法であるように思われるものは以下のものです。

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

私は正しいディレクトリにXSDファイルも持っていませんでした。

うまくいけば、これは他の誰かに役立ちます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top