我能够按照示例代码更改 1 个元素值,但不知道如何继续更改其他元素文本。
void setXML(unsigned short voltage_value, unsigned int current_value){
XMLError eresult = xmlDoc.LoadFile("SavedData.xml");
if (eresult != XML_SUCCESS){
printf("Error: %in", eresult);
}
XMLNode * pRoot = xmlDoc.FirstChild();
XMLElement * pElement = pRoot->FirstChildElement("Voltage");
if (pElement == 0) {
printf("Error");
}
else{
pElement->SetText(voltage_value);
//xmlDoc.SaveFile("/var/www/html/SavedData.xml");
}
pElement = pElement->NextSiblingElement("Current");
if (pElement == 0) {
printf("Error");
}
else{
pElement->SetText(current_value);
}
xmlDoc.SaveFile("/var/www/html/SavedData.xml");
}
<Battery_1>
<Voltage>13.5</Voltage>
<Current>1.5</Current>
<Watt>22.5</Watt>
<AmpHr>3.5</AmpHr>
<Time>79345</Time>
<Date day="11" month="7" year="2019"/>
</Battery_1>
<Battery_2>
<Voltage>13.8</Voltage>
<Current>1.4</Current>
<Watt>20.5</Watt>
<AmpHr>3.1</AmpHr>
<Time>79345</Time>
<Date day="11" month="7" year="2019"/>
</Battery_2>
实际上,我将要更新电池标签以外的所有值。FirstChildElement("Voltage")
总是找到,我可以更改其文本,无法弄清楚如何转到下一个元素或只是随机转到第 4 个元素"AmpHr"
并更改其值/文本。 第二个pElement
总是 0,所以它只是打印错误; 希望使其尽可能简单,可读且易于理解,我是新手。
设法完成了它,但我不喜欢分叉,如果这是 2 个元素中的 6 个元素:( 我继续研究它并学习它:)
void setXML(unsigned short voltage_value, unsigned int current_value){
XMLError eresult = xmlDoc.LoadFile("/var/www/html/SavedData.xml");
if (eresult != XML_SUCCESS){
printf("Error: %in", eresult);
}
XMLNode * pRoot = xmlDoc.FirstChildElement("Asset");
if(pRoot){
XMLElement * pElement = pRoot->FirstChildElement("Battery_1");
if(pElement){
XMLElement * pChild = pElement->FirstChildElement("Voltage");
if(pChild){
pChild->SetText(voltage_value);
XMLElement * pChild2 = pChild->NextSiblingElement("Current");
if(pChild2){
pChild2->SetText(current_value);
xmlDoc.SaveFile("/var/www/html/SavedData.xml");
}
}
}
}
<?xml version="1.0" ?>
<Asset>
<Battery_1>
<Voltage>1385</Voltage>
<Current>1500</Current>
<Watt>225</Watt>
<AmpHr>3500</AmpHr>
<Time>79355</Time>
<Date day="11" month="7" year="2019"/>
</Battery_1>
<Battery_2>
<Voltage>1321</Voltage>
<Current>1105</Current>
<Watt>225</Watt>
<AmpHr>3125</AmpHr>
<Time>77345</Time>
<Date day="11" month="7" year="2019"/>
</Battery_2>
<Battery_3>
<Voltage>1382</Voltage>
<Current>1535</Current>
<Watt>225</Watt>
<AmpHr>3745</AmpHr>
<Time>78345</Time>
<Date day="11" month="7" year="2019"/>
</Battery_3>
</Asset>
更改了 XML 以使用属性而不是多个元素,在我的情况下工作得很好。 也许有一天有人发现它很有用:)
void setXML(unsigned short voltage, unsigned short current,
unsigned int watt, unsigned int amphr, unsigned int watthr, unsigned int time){
XMLError eresult = xmlDoc.LoadFile("/var/www/html/SavedData1.xml");
if (eresult != XML_SUCCESS){
printf("Error: %in", eresult);
}
else{
XMLNode * pRoot = xmlDoc.FirstChildElement("Asset");
if(pRoot){
XMLElement * pElement = pRoot->FirstChildElement("Battery_1");
if(pElement){
pElement->SetAttribute("voltage", voltage);
pElement->SetAttribute("current", current);
pElement->SetAttribute("watt", watt);
pElement->SetAttribute("amphr", amphr);
pElement->SetAttribute("watthr", watthr);
pElement->SetAttribute("time", time);
}
}
}
}
<Asset>
<Battery_1 voltage="1390" current="0" watt="0" amphr="2462" watthr="34174" time="217610"/>
<Battery_2 voltage="1483" current="700" watt="2500" amphr="1500" watthr="7643" time="79355"/>
<Battery_3 voltage="1483" current="700" watt="2500" amphr="1500" watthr="34557" time="79355"/>
</Asset>
使用 JavaScript 通过 Web 读取数据
var batData = xml.getElementsByTagName('Battery_1');
txt = batData[0].getAttribute("voltage")/100 + " Volt<br>";
txt += batData[0].getAttribute("current")/100 + " Amp<br>";
txt += batData[0].getAttribute("watt")/1000 + " Watt<br>";
txt += batData[0].getAttribute("amphr")/1000 + " AmpHR<br>";
txt += batData[0].getAttribute("watthr")/1000 + " WattHR<br>";
txt += batData[0].getAttribute("time") + " Time in Seconds<br>";