c++ nkit库操作xml

2022-02-17  本文已影响0人  一路向后

1.源码实现

#include <nkit/dynamic_json.h>
#include <nkit/logger_brief.h>
#include <nkit/dynamic/dynamic_builder.h>
#include <nkit/dynamic_xml.h>

using namespace std;
using namespace nkit;

int main()
{
    Dynamic options;
    Dynamic options2 = options = DDICT(
        "rootname" << "node"
        << "version" << "1.0"
        << "encoding" << "ISO-8859-1"
    );

    Dynamic data = DDICT("rootname" << "ROOT"
        << "itemname" << "item"
        << "encoding" << "UTF-8");
    string out;

    Dynamic2XmlConverter::Process(options, data, &out, NULL);

    cout << data["rootname"] << endl;

    cout << out << endl;

    string xml_path = "./2.xml";
    string xml;
    string mapping = "{ \"to\": \"string\" }";
    string error;

    text_file_to_string(xml_path, &xml, &error);

    Dynamic var = DynamicFromXml(xml, options, mapping, &error);

    cout << var["to"] << endl;

    return 0;
}

2.2.xml

<?xml version="1.0" encoding="ISO-8859-1"?><note><to>George</to></note>

3.编译源码

$ g++ -o test test.cpp xml2var.cpp dynamic_xml.cpp -std=c++11 -lnkit -Wl,-rpath=/usr/local/lib -lyajl -lexpat

4.运行及其结果

$ ./test
"ROOT"
<node><encoding>UTF-8</encoding><itemname>item</itemname><rootname>ROOT</rootname></node>
"George"
上一篇下一篇

猜你喜欢

热点阅读