How to write an XML file from an XSD cxx tree object? - c++

I am working with a library that uses XSD for creating objects from their XML-based format in C++.
Basically, the inheritance diagram looks something like this:
class BaseClass: public xsd::cxx::tree::type;
class MainXmlObject: public BaseClass;
I think I need to convert my MainXmlObject to a xerces::DOMDocument and then use DOMWriter to write the actual XML file, but I couldn't find the right routines so far.
What is the best way to do this?

It seems like adding the --add-serialization flag to the xsd code generation and then using something like:
xml_schema::namespace_infomap map;
// map[""].name = "test"; // xmlns
// map[""].schema = "http://sbgn.org/libsbgn/0.2"; // xsi:noNamespaceSchemaLocation
ofstream ofs(fname.c_str());
sbgn_(ofs, s, map); // invoking the stream here
ofs.close();
works. References: Adding serialization and details from the XSD guide.

Related

How would you auto generate a class implementation during pre-compile?

I was looking at creating a service locator that I would also like to provide "NULL" implementations of the services it provides. How would you go about parsing the Interface and auto generating an implementation? What tools would you use - say with CMake? Basically, I am trying to avoid having to write something like:
class IAudio
{
virtual void playSound(SoundId Id) = 0;
....
};
***** Following is the boilerplate I would like to avoid *****
class NullAudio : IAudio
{
void playSound(SoundId) override { /* Does Nothing */ }
....
};
I can't seem to find any examples from my google searches for automatic code generation - they all turn up things that reference code completion in editors. I would even consider running a python script that looks for files starting with I - like IAudio.hpp, parses it and writes out a file if that is the common way to do this.
Thanks!
I think I figured it out. Write a small tool to do what I want, configure CMake to compile it first, and then have CMake run the tool with add_custom_command.

Serializing a FlatBuffer object to JSON without it's schema file

I've been working with FlatBuffers as a solution for various things in my project, one of them specifically being JSON support. However, while FB natively supports JSON generation, the documentation for flatbuffers is poor, and the process is somewhat cumbersome. Right now, I am working in the Object->JSON direction. The issue I am having doesn't really arise the other way around (I think).
I currently have JSON generation working per an example I found here (line 630, JsonEnumsTest()) - by parsing a .fbs file into a flattbuffers::Parser, building and packaging my flatbuffer object, then running GenerateText() to generate a JSON string. The code I have is simpler than the example in test.cpp, and looks vaguely like this:
bool MyFBSchemaWrapper::asJson(std::string& jsonOutput)
{
//**This is the section I don't like having to do
std::string schemaFile;
if (flatbuffers::LoadFile((std::string(getenv("FBS_FILE_PATH")) + "MyFBSchema.fbs").c_str(), false, &schemaFile))
{
flatbuffers::Parser parser;
const char *includePaths[] = { getenv("FBS_FILE_PATH");
parser.Parse(schemaFile.c_str(), includePaths);
//**End bad section
parser.opts.strict_json = true;
flatbuffers::FlatBufferBuilder fbBuilder;
auto testItem1 = fbBuilder.CreateString("test1");
auto testItem2 = fbBuilder.CreateString("test2");
MyFBSchemaBuilder myBuilder(fbBuilder);
myBuilder.add_item1(testItem1);
myBuilder.add_item2(testItem2);
FinishMyFBSchemaBuffer(fbBuilder, myBuilder.finish());
auto result = GenerateText(parser, fbBuilder.GetBufferPointer(), &jsonOutput);
return true;
}
return false;
}
Here's my issue: I'd like to avoid having to include the .fbs files to set up my Parser. I don't want to clutter an already large monolithic program by adding even more random folders, directories, environment variables, etc. I'd like to be able to generate JSON from the compiled FlatBuffer schemas, and not have to search for a file to do so.
Is there a way for me to avoid having to read back in my .fbs schemas into the parser? My intuition is pointing to no, but the lack of documentation and community support on the topic of FlatBuffers & JSON is telling me there might be a way. I'm hoping that there's a way to use the already generated MyFBSchema_generated.h to create a JSON string.
Yes, see Mini Reflection in the documentation: http://google.github.io/flatbuffers/flatbuffers_guide_use_cpp.html

ArrayOfXXX class out of soap input param of array type

I have a method with input param as Array. When I generate stub out of it creates List type.
But I want to know how to create a wrapper class around array type e.g. for class Apple it should create ArrayOfApple.
Is there any change needs to be done in class or any specific plugin need to be used?
Note: I am using JAXWS with Apache CXF implementation
Below is the sample code:
EmployeeService.java:
#WebService(endpointInterface="com.test.EmployeeService")
#SOAPBinding(style=Style.DOCUMENT)
public class EmployeeService {
public String updateEmpRoles(#WebParam(name="EmpRoles")EmpRole[] empRoles) {
return "SUCCESS";
}
}
EmpRole.java :
#XmlType(name="EmpRole")
public class EmpRole {
private String empRole;
public String getEmpRole() {
return empRole;
}
public void setEmpRole(String empRole) {
this.empRole = empRole;
}
}
After publishing, wsdl is getting generated as below -
But what I expect is WSDL should create ArrayOfEmpRole and it should wrap List<EmpRole>.
Kindly help
In short - I want something that Björn doesn't want in below link. (In his case, it's automatically creating ArrayOfXXX, this is what I need) - Arrays in SOAP method Parameters generated via JAX-WS?
I would switch from Code first to a Contract first approach which means start with the WSDL and generate a stub using wsdl2java from it. This way you can ensure that the WSDL looks like the way you want.
If you want to stick to the current approach, the easiest way to achieve a wrapper is probably to introduce another class.

how to create a JSON file with c++ methods

We are doing a black box testing in our project,For that we wanted to use json and c++ and Jzon for parsing.can anybody show how to write a json file for c++ method and parse to get the node by using jzon ?
To write Json object (Jzon::Node) to file you can use Jzon::Writer class.
There are three methods writeStream,writeString and writeFile. Based on target where you want to save your object you can you appropriate method. To save into file you should use writeFile.
You code should be like this.
std::string filename="some path and file name.json";
Jzon::Node node;
//fill your object
Jzon::Writer write;
write.writeFile(node,filename);

Is it possible to use TinyXML2 (or any other library) to convert structure or class directly to XML file and viceversa?

I have number of structures that look like this:
struct A
{
long lValueA;
BOOL bValueA;
CString strValueA;
};
struct B
{
long lValueB;
BOOL bValueB;
CString strValueB;
};
struct C
{
A a;
vector<B> vecB;
};
Is it possible to use TinyXML2 (or any other library) to convert it into XML file without manually pass each member variable from struct C? what I want will look like this:
main()
{
C c;
// Some code to initialise member variable of struct C
// pass object/structure to XML parser to get XML file.
Some_XML_Library_Object.parse( c );
Some_XML_Library_Object.SaveFile("FilePath/Name.xml");
// Also it would be nice if we can update values in structure or class directly like this
const char* XML_File_Path = "FilePath/Name.xml";
Some_XML_Library_Object.updateValueOfStructureFromXML(&c,XML_File_Path)
}
The XML File produce look similar to this:
<?xml version="1.0" encoding="UTF-8"?>
<A>
<lValueA>
value
</lValueA>
<bValueA>
value
</bValueA>
<strValueA>
value
</strValueA>
</A>
<B>
<lValueB>
value
</lValueB>
...
...
</B>
Thanks in advance.
You could use boost::serialisation to do something like what you asked...
http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/tutorial.html
it has extensive capabilities for handling XML (and other formats) from the above page:
In this tutorial, we have used a particular archive class -
text_oarchive for saving and text_iarchive for loading. text archives
render data as text and are portable across platforms. In addition to
text archives, the library includes archive class for native binary
data and xml formatted data. Interfaces to all archive classes are all
identical. Once serialization has been defined for a class, that class
can be serialized to any type of archive.
Edit: you'd handle objects and serialize them rather than manipulate the XML directly to change values.