您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。