struct PUBLIC_DATA_SEND
{
bool ifrealtimedata;
bool ifdatasend;
bool confirmflag;
int sendtimes;
int sendtime;
QList waitconfirmterminal;
QList 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
{
QDomElement meter = doc->createElement("meter");
meter.setAttribute("id", "445");
for ( int j = 0; 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+
strcpy(buf, doc->toString().replace(QRegExp(">\\s+
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内容为
125479601
99
energy_data
2017-03-13 15:50
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
123.45864
和12547960199energy_data2017-03-13 15:50123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864123.45864