I've used boost serialization but this doesn't appear to allow me to generate xml that conforms to a particular schema -- it seems it's purpose was to just to persist a class's state.
我使用了boost序列化,但这似乎不允许生成符合特定模式的xml——它的目的似乎只是为了持久化类的状态。
Platform: linux
linux平台:
What do you guys use to generate NOT parse xml?
你们用什么来生成不解析xml?
So far I'm going down Foredecker's route of just generating it myself -- it's not a large document but I really shouldn't be having this much trouble finding a decent library to generate it correctly.
到目前为止,我只是按照前德克的方式自己生成它——它不是一个大文档,但我真的不应该在找到一个合适的库来正确生成它时遇到这么多麻烦。
As for boost, the things that I would like to be able to do is set the node names, set attributes in my nodes, and get rid of all the extra crap that comes with it as I don't really care about having to put my document back into that class.
至于提高,我希望能够做的就是设置节点名,设置属性在我的节点,摆脱一切多余的废话,这是我真的不关心不得不把我的文档回班。
6 个解决方案
#1
10
Some may declare me an XML heretic - but one effective way is to just generate it with your favorite string output tools (print, output streams, etc) - this can go to a buffer or a file.
有些人可能会说我是XML异端——但是一种有效的方法是使用您喜欢的字符串输出工具(打印、输出流等)生成它——这可以到缓冲区或文件中。
Once saved - you really should then validate with a schema before committing it our shipping it off.
一旦被保存,您真的应该在提交它之前对模式进行验证。
For one of our projects we have a very simple set of templates for managing begin/end tags and attributes. These each have a stream output operator. This makes it very easy to generate the source XML and debug. This makes the structure of the XML generation code look very much like the XML itself.
对于我们的一个项目,我们有一组非常简单的模板来管理开始/结束标记和属性。它们都有一个流输出操作符。这使得生成源XML和调试非常容易。这使得XML生成代码的结构看起来非常像XML本身。
One advantage of this is that you can generate large amounts of XML efficiently if streaming to a file. You will pay the validation costs later (presumably at a better time for an expensive operation).
这样做的一个优点是,如果流到文件中,您可以有效地生成大量XML。稍后您将支付验证成本(可能是在成本较高的时候)。
The downside of this technique is that it is essentially output only. It is not suitable for creating then consuming XML dynamically.
这种技术的缺点是它本质上只是输出。它不适合动态创建然后使用XML。
#2
48
I recently reviewed a bunch of XML libraries specifically for generating XML code.
我最近回顾了一些专门用于生成XML代码的XML库。
Executive summary: I chose to go with TinyXML++.
执行概要:我选择了TinyXML++。
TinyXML++ has decent C++ syntax, is built on the mature TinyXML C libraries, is free & open source (MIT license) and small. In short, it helps get the job done quickly. Here's a quick snippet:
TinyXML++具有良好的c++语法,构建在成熟的TinyXML C库上,是免费开放源码(MIT许可)和small。简而言之,它有助于迅速完成工作。这里有一个快速的片段:
Document doc;
Node* root(doc.InsertEndChild(Element("RootNode")));
Element measurements("measurements");
Element tbr("TotalBytesReceived", 12);
measurements.InsertEndChild(tbr);
root->InsertEndChild(measurements);
Which produces:
生产:
<RootNode>
<measurements>
<TotalBytesReceived>12</TotalBytesReceived>
</measurements>
</RootNode>
I've been quite happy with it.
我对此很满意。
I reviewed many others; here's some of the better contenders:
我回顾了许多人;以下是一些更好的竞争者:
Xerces: The king-daddy. Does everything (especially when combined with Xalan) but is heavyweight and forces memory management onto the user.
Xerces:国王爸爸。做所有的事情(特别是与Xalan结合时),但是是重量级的,并且强制用户进行内存管理。
RapidXML: Great for parsing (it's an in-situ parser and is fast) but not good for generation since adding nodes to the DOM requires memory management.
RapidXML:非常适合解析(它是一个就地解析器,速度很快),但是对于生成来说不太好,因为向DOM添加节点需要内存管理。
Boost.XML (proposal): Looks great - powerful, excellent C++ syntax. However it hasn't yet gone through the review process, is unsupported and the interface may well change. Almost used it anyway. Looking forward to it's acceptance into Boost.
提振。XML(建议):看起来很强大,很好的c++语法。然而,它还没有经过审查过程,不受支持,接口很可能会发生变化。几乎用它。期待它在Boost中得到认可。
Libxml(++): Very good; powerful, decent syntax. But it's large-ish if all you're doing is generating XML and is tied to the glibmm library (for ustring). If we were only on Linux (like yourself?) I would seriously consider.
Libxml(+ +):很好;强大的、体面的语法。但是,如果您所做的只是生成XML并与glibmm库(用于ustring)绑定,那么它就有点大了。如果我们只是在Linux上(像你一样?)我会认真考虑的。
XiMOL: Unique stream-based library. This was a little too simplistic for our needs but for basic XML generation you may find it quite useful. The stream syntax is quite neat.
XiMOL:独特的基于流的图书馆。这对于我们的需要来说有点过于简单了,但是对于基本的XML生成,您可能会发现它非常有用。流语法非常简洁。
Hopefully there's something in there of some use!
希望里面有一些有用的东西!
#3
9
Boost.PropertyTree is a nice and straightforward way of generating XML - especially if you are already using Boost.
提振。PropertyTree是一种生成XML的好方法,特别是如果您已经在使用Boost的话。
The following is a complete example program:
以下是一个完整的示例程序:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;
using boost::property_tree::write_xml;
using boost::property_tree::xml_writer_settings;
int wmain(int argc, wchar_t* argv[]) {
char* titles[] = {"And Then There Were None", "Android Games", "The Lord of the Rings"};
ptree tree;
tree.add("library.<xmlattr>.version", "1.0");
for (int i = 0; i < 3; i++) {
ptree& book = tree.add("library.books.book", "");
book.add("title", titles[i]);
book.add("<xmlattr>.id", i);
book.add("pageCount", (i+1) * 234);
}
// Note that starting with Boost 1.56, the template argument must be std::string
// instead of char
write_xml("C:\\Users\\Daniel\\Desktop\\test.xml", tree,
std::locale(),
xml_writer_settings<char>(' ', 4));
return 0;
}
The resulting XML looks like this:
得到的XML如下所示:
<?xml version="1.0" encoding="utf-8"?>
<library version="1.0">
<books>
<book id="0">
<title>And Then There Were None</title>
<pageCount>234</pageCount>
</book>
<book id="1">
<title>Android Games</title>
<pageCount>468</pageCount>
</book>
<book id="2">
<title>The Lord of the Rings</title>
<pageCount>702</pageCount>
</book>
</books>
</library>
One thing that's particularly nice are the dot-separated paths that allow you to implicitly create all the nodes along the way. The documentation is rather meager, but together with ptree.hpp
should give you an idea of how it works.
有一件特别好的事情是,允许你在路上隐式地创建所有节点的点分隔路径。文件相当贫乏,但与ptree一起。hpp应该会告诉你它是如何工作的。
#4
0
Which platform? MSXML is an option on Windows. CMarkup is another good choice. If .NET is an option, it has excellent XML support.
在哪个站台?MSXML是Windows上的一个选项。CMarkup是另一个很好的选择。如果. net是一个选项,那么它有很好的XML支持。
#5
0
I haven't actually tried boost serialization to do this, but as I understand it, if you very carefully pick your options, you can set lots of different options in the schema (such as preventing pointer flyweighting and setting the element names for certain types)
实际上,我还没有尝试过推进序列化,但是据我所知,如果您非常仔细地选择您的选项,您可以在模式中设置许多不同的选项(例如防止指针飞重和为某些类型设置元素名称)
#6
0
i use tinyXml++ and it makes it very easy to create xml as posted above. It will also save it to a file in one command but i cant find out how to save it to a buffer.
我使用tinyXml++,这使得创建上面发布的xml变得非常容易。它还将把它保存到一个命令的文件中,但我无法找到如何将其保存到缓冲区的方法。
#1
10
Some may declare me an XML heretic - but one effective way is to just generate it with your favorite string output tools (print, output streams, etc) - this can go to a buffer or a file.
有些人可能会说我是XML异端——但是一种有效的方法是使用您喜欢的字符串输出工具(打印、输出流等)生成它——这可以到缓冲区或文件中。
Once saved - you really should then validate with a schema before committing it our shipping it off.
一旦被保存,您真的应该在提交它之前对模式进行验证。
For one of our projects we have a very simple set of templates for managing begin/end tags and attributes. These each have a stream output operator. This makes it very easy to generate the source XML and debug. This makes the structure of the XML generation code look very much like the XML itself.
对于我们的一个项目,我们有一组非常简单的模板来管理开始/结束标记和属性。它们都有一个流输出操作符。这使得生成源XML和调试非常容易。这使得XML生成代码的结构看起来非常像XML本身。
One advantage of this is that you can generate large amounts of XML efficiently if streaming to a file. You will pay the validation costs later (presumably at a better time for an expensive operation).
这样做的一个优点是,如果流到文件中,您可以有效地生成大量XML。稍后您将支付验证成本(可能是在成本较高的时候)。
The downside of this technique is that it is essentially output only. It is not suitable for creating then consuming XML dynamically.
这种技术的缺点是它本质上只是输出。它不适合动态创建然后使用XML。
#2
48
I recently reviewed a bunch of XML libraries specifically for generating XML code.
我最近回顾了一些专门用于生成XML代码的XML库。
Executive summary: I chose to go with TinyXML++.
执行概要:我选择了TinyXML++。
TinyXML++ has decent C++ syntax, is built on the mature TinyXML C libraries, is free & open source (MIT license) and small. In short, it helps get the job done quickly. Here's a quick snippet:
TinyXML++具有良好的c++语法,构建在成熟的TinyXML C库上,是免费开放源码(MIT许可)和small。简而言之,它有助于迅速完成工作。这里有一个快速的片段:
Document doc;
Node* root(doc.InsertEndChild(Element("RootNode")));
Element measurements("measurements");
Element tbr("TotalBytesReceived", 12);
measurements.InsertEndChild(tbr);
root->InsertEndChild(measurements);
Which produces:
生产:
<RootNode>
<measurements>
<TotalBytesReceived>12</TotalBytesReceived>
</measurements>
</RootNode>
I've been quite happy with it.
我对此很满意。
I reviewed many others; here's some of the better contenders:
我回顾了许多人;以下是一些更好的竞争者:
Xerces: The king-daddy. Does everything (especially when combined with Xalan) but is heavyweight and forces memory management onto the user.
Xerces:国王爸爸。做所有的事情(特别是与Xalan结合时),但是是重量级的,并且强制用户进行内存管理。
RapidXML: Great for parsing (it's an in-situ parser and is fast) but not good for generation since adding nodes to the DOM requires memory management.
RapidXML:非常适合解析(它是一个就地解析器,速度很快),但是对于生成来说不太好,因为向DOM添加节点需要内存管理。
Boost.XML (proposal): Looks great - powerful, excellent C++ syntax. However it hasn't yet gone through the review process, is unsupported and the interface may well change. Almost used it anyway. Looking forward to it's acceptance into Boost.
提振。XML(建议):看起来很强大,很好的c++语法。然而,它还没有经过审查过程,不受支持,接口很可能会发生变化。几乎用它。期待它在Boost中得到认可。
Libxml(++): Very good; powerful, decent syntax. But it's large-ish if all you're doing is generating XML and is tied to the glibmm library (for ustring). If we were only on Linux (like yourself?) I would seriously consider.
Libxml(+ +):很好;强大的、体面的语法。但是,如果您所做的只是生成XML并与glibmm库(用于ustring)绑定,那么它就有点大了。如果我们只是在Linux上(像你一样?)我会认真考虑的。
XiMOL: Unique stream-based library. This was a little too simplistic for our needs but for basic XML generation you may find it quite useful. The stream syntax is quite neat.
XiMOL:独特的基于流的图书馆。这对于我们的需要来说有点过于简单了,但是对于基本的XML生成,您可能会发现它非常有用。流语法非常简洁。
Hopefully there's something in there of some use!
希望里面有一些有用的东西!
#3
9
Boost.PropertyTree is a nice and straightforward way of generating XML - especially if you are already using Boost.
提振。PropertyTree是一种生成XML的好方法,特别是如果您已经在使用Boost的话。
The following is a complete example program:
以下是一个完整的示例程序:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;
using boost::property_tree::write_xml;
using boost::property_tree::xml_writer_settings;
int wmain(int argc, wchar_t* argv[]) {
char* titles[] = {"And Then There Were None", "Android Games", "The Lord of the Rings"};
ptree tree;
tree.add("library.<xmlattr>.version", "1.0");
for (int i = 0; i < 3; i++) {
ptree& book = tree.add("library.books.book", "");
book.add("title", titles[i]);
book.add("<xmlattr>.id", i);
book.add("pageCount", (i+1) * 234);
}
// Note that starting with Boost 1.56, the template argument must be std::string
// instead of char
write_xml("C:\\Users\\Daniel\\Desktop\\test.xml", tree,
std::locale(),
xml_writer_settings<char>(' ', 4));
return 0;
}
The resulting XML looks like this:
得到的XML如下所示:
<?xml version="1.0" encoding="utf-8"?>
<library version="1.0">
<books>
<book id="0">
<title>And Then There Were None</title>
<pageCount>234</pageCount>
</book>
<book id="1">
<title>Android Games</title>
<pageCount>468</pageCount>
</book>
<book id="2">
<title>The Lord of the Rings</title>
<pageCount>702</pageCount>
</book>
</books>
</library>
One thing that's particularly nice are the dot-separated paths that allow you to implicitly create all the nodes along the way. The documentation is rather meager, but together with ptree.hpp
should give you an idea of how it works.
有一件特别好的事情是,允许你在路上隐式地创建所有节点的点分隔路径。文件相当贫乏,但与ptree一起。hpp应该会告诉你它是如何工作的。
#4
0
Which platform? MSXML is an option on Windows. CMarkup is another good choice. If .NET is an option, it has excellent XML support.
在哪个站台?MSXML是Windows上的一个选项。CMarkup是另一个很好的选择。如果. net是一个选项,那么它有很好的XML支持。
#5
0
I haven't actually tried boost serialization to do this, but as I understand it, if you very carefully pick your options, you can set lots of different options in the schema (such as preventing pointer flyweighting and setting the element names for certain types)
实际上,我还没有尝试过推进序列化,但是据我所知,如果您非常仔细地选择您的选项,您可以在模式中设置许多不同的选项(例如防止指针飞重和为某些类型设置元素名称)
#6
0
i use tinyXml++ and it makes it very easy to create xml as posted above. It will also save it to a file in one command but i cant find out how to save it to a buffer.
我使用tinyXml++,这使得创建上面发布的xml变得非常容易。它还将把它保存到一个命令的文件中,但我无法找到如何将其保存到缓冲区的方法。