struct PUBLIC_DATA_SEND { bool ifrealtimedata; bool ifdatasend; bool confirmflag; int sendtimes; int sendtime; QList<QString> waitconfirmterminal; QList<QString> DateTimeList; char capturetime[20]; }SEND; int package_public_data(QDomDocument *doc, PUBLIC_DATA_SEND *ppublicdata, int client_num, int datatype, QDomElement *root, QDomElement *data) { QDomElement meters = doc->createElement("meters"); meters.setAttribute("total", "0"); QDomText textNode; for ( int i = 0; i < 10; i++) { QDomElement meter = doc->createElement("meter"); meter.setAttribute("id", "445"); for ( int j = 0; j < 5; j++ ) { QDomElement var = doc->createElement("var"); var.setAttribute("e", ""); var.setAttribute("e", "1"); textNode = doc->createTextNode("123.45864"); var.appendChild(textNode); meter.appendChild(var); } meters.appendChild(meter); } meters.setAttribute("total", "10"); data->appendChild(meters); return 0; } int publicdata_xm(int flag, PUBLIC_DATA_SEND *ppublicdata, char *buf, int client_num, char *name) { QDomDocument *doc = new QDomDocument(); QDomProcessingInstruction instruction; QDomText textNode; instruction = doc->createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\""); QDomElement root = doc->createElement("root"); QDomElement common = doc->createElement("common"); QDomElement building_id = doc->createElement("building_id"); QDomElement gateway_id = doc->createElement("gateway_id"); QDomElement type = doc->createElement("type"); QDomElement data = doc->createElement("data"); QDomElement time = doc->createElement("time"); doc->appendChild(root); root.appendChild(common); common.appendChild(building_id); common.appendChild(gateway_id); common.appendChild(type); root.appendChild(data); data.appendChild(time); textNode = doc->createTextNode("125479601"); building_id.appendChild(textNode); textNode = doc->createTextNode("99"); gateway_id.appendChild(textNode); textNode = doc->createTextNode("energy_data"); type.appendChild(textNode); QString datetime = "2017-03-13 15:50"; textNode = doc->createTextNode(datetime); time.appendChild(textNode); if ( flag == 1 ) { data.setAttribute("operation", "report"); } else if ( flag == 2 ) { data.setAttribute("operation", "continuous"); } else if ( flag == 3 ) { data.setAttribute("operation", "history"); } package_public_data(doc, ppublicdata, client_num, flag, &root, &data); printf("publicdata xml:\n%s\n",QString::fromLocal8Bit(doc->toByteArray()).toUtf8().data()); int len = doc->toString().replace(QRegExp(">\\s+<"),"><").toLatin1().length(); strcpy(buf, doc->toString().replace(QRegExp(">\\s+<"),"><").toLatin1()); delete doc; return len; } int main(int argc, char *argv[]) { char buf[4096]; int len = 0; len = publicdata_xm(1, &SEND, buf, 1, "dom"); printf("dom xml len = %d: \n%s\n", len, buf); return 0; }
DOM格式的xml,使用内存大,建议使用在xml内容小的情况下,dom格式的xml为树状结构,可以随意删除修改xml
输出xml内容为
<root> <common> <building_id>125479601</building_id> <gateway_id>99</gateway_id> <type>energy_data</type> </common> <data operation="report"> <time>2017-03-13 15:50</time> <meters total="10"> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> <meter id="445"> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> <var e="1">123.45864</var> </meter> </meters> </data> </root>
和
<root><common><building_id>125479601</building_id><gateway_id>99</gateway_id><type>energy_data</type></common><data operation="report"><time>2017-03-13 15:50</time><meters total="10"><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter><meter id="445"><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var><var e="1">123.45864</var></meter></meters></data></root>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。