Hello Andreas,

To add "Header" and "Body" into the xml, you have to modify the serializer:

void operator<<= (cxxtools::SerializationInfo &si, const AMessage& obj)
{
      si.setTypeName("AMessage");  // it is not necessary but always 
good to set the type name btw.
      si.addMember("Header").addMember("headerField") <<= obj.headerField;
      si.addMember("Body").addMember("bodyField") <<= obj.bodyField;
}

As an alternative you may want to create separate structs for "Header" 
and "Body". Like this:

struct Header {
     std::string headerField;
};

struct Body {
     std::string bodyField;
};

struct AMessage {
     Header header;
     Body body;
};

void operator<<= (cxxtools::SerializationInfo &si, const Header& obj)
{
      si.setTypeName("Header");
      si.addMember("headerField") <<= obj.headerField;
}

void operator<<= (cxxtools::SerializationInfo &si, const Body& obj)
{
      si.setTypeName("Body");
      si.addMember("bodyField") <<= obj.bodyField;
}

void operator<<= (cxxtools::SerializationInfo &si, const AMessage& obj)
{
      si.setTypeName("AMessage");
      si.addMember("Header") <<= obj.header;
      si.addMember("Body") <<= obj.body;
}

Te get rid of "category=..." and "type=..." you can reset a flag:

      cxxtools::xml::XmlSerializer serializer(std::cout);
      serializer.useAttributes(false);
      serializer.serialize(msg, "AMessage");
      serializer.finish();

There are also flags for disabling the xml declaration header 
(useXmlDeclaration), indentation (useIndent) and line feeds (useEndl).

Note that if you want to disable the xml declaration, it needs to be 
done before the xml declaration is sent to the out stream, which happens 
as soon as the out stream is set. You have to use the method "attach" 
instead of the constructor to set it after setting the proper attribute:

      cxxtools::xml::XmlSerializer serializer
      serializer.useXmlDeclaration(false);
      serializer.attach(std::cout);

Hope that helps,


Tommi

Am 28.05.2014 13:12, schrieb Andreas Welchlin:
> Hi All,
>
> I would like to build an XML structure which was defined by a third party.
>
> It should look like
>
> <?xml version="1.0" encoding="utf-8"?>
> <AMessage>
>     <Header>
>         <headerField>ttt</headerField>
>     </Header>
>     <Body>
>         <bodyField>aaa</bodyField>
>     </Body>
> </AMessage>
>
> The result I get using cxxtools is at the moment:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <AMessage category="struct">
>     <headerField type="string">ttt</headerField>
>     <bodyField type="string">aaa</bodyField>
> </AMessage>
>
>
> My questions are:
>
> - How can I insert Header and Body into the hierarchy?
> - Can I get rid of 'category="struct"'?
> - Can I get rid of 'type="string"'?
>
> And in general what is the best way to manipulate the result when I need
> changes in the format?
>
>
>
>
> My code looks like this:
> ====================
>
> // Business Object
> struct AMessage {
>     std::string headerField;
>     std::string bodyField;
> };
>
> //  Operator for the serializer.
> void operator<<= (cxxtools::SerializationInfo &si, const AMessage& obj)
> {
>       si.addMember("headerField") <<= obj.headerField;
>       si.addMember("bodyField") <<= obj.bodyField;
> }
>
> // Serialize to xml
> main()
> {
>       AMessage msg;
>       msg.headerField = "ttt";
>       msg.bodyField = "aaa";
>
>       std::ostringstream xmlstream;
>       cxxtools::xml::XmlSerializer serializer(xmlstream);
>       serializer.serialize(msg, "AMessage");
>       serializer.finish();
>       log_info(xmlstream.str());
> }
>
>
> Kind Regards,
> Andreas
>
>


------------------------------------------------------------------------------
Time is money. Stop wasting it! Get your web API in 5 minutes.
www.restlet.com/download
http://p.sf.net/sfu/restlet
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to