我从XSD文件生成了一个带有CodeSynthesis的C ++类。现在我想创建一个C ++对象,用数据填充它并将其序列化为XML或JSON格式。 如何生成XML?我也使用框架Qt。它应该看起来像JAXB。例如(JavaCode):
Worker w = new Worker();
w.setName("Peter");
w.setStreet("...");
String xml = "XMLGenerator".generate(w);`
这可能吗?
答案 0 :(得分:1)
是的,这是可能的。
CodeSynthesis XSD 的文档中有一个hello world示例,其中创建了C ++对象,然后将其序列化为XML。
了解更多信息:
C++/Tree Mapping Getting Started Guide( 2.5添加序列化一节
)答案 1 :(得分:0)
您可以使用DOM类在Qt中创建XML文件。 DOM的工作原理是将整个XML文档表示为内存中节点对象的树。 :
QDomDocument document;
QDomElement d = document.createElement( "document" );
d.setAttribute( "name", "DocName" );
QDomElement a = document.createElement( "author" );
a.setAttribute( "name", "AuthorName" );
QDomText text = document.createTextNode( "Some text" );
document.appendChild( d );
d.appendChild( a );
d.appendChild( text );
//Writing to a file
QFile file( "simple.xml" );
if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
qDebug( "Failed to open file for writing." );
return -1;
}
QTextStream stream( &file );
stream << document.toString();
file.close();