0

I was able to follow example code to change 1 element value but dont know how to proceed changing other elements text.

void setXML(unsigned short voltage_value, unsigned int current_value){
    XMLError eresult = xmlDoc.LoadFile("SavedData.xml");
    if (eresult != XML_SUCCESS){
        printf("Error: %i\n", 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>

Actually I will want to update all values other than the battery tag. FirstChildElement("Voltage") always found and I can change its text, can't figure out how to go to next element or just randomly for example to the 4th "AmpHr" and change its value/text. 2nd pElement always 0 so its just print error; Looking to make it simple as possible, readable and easy to understand, I'm new to this.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Elcast
  • 59
  • 2
  • 8

2 Answers2

0

Managed to get it done but I don't like the forking if and this is jut 2 element out of 6 :( Ill keep working on it and learning it :)

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: %i\n", 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>
Elcast
  • 59
  • 2
  • 8
0

Changed the XML to use Attribute instead of multiple Elements, in my case works just fine. Maybe someone find it useful one day :)

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: %i\n", 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>

To read the data over web using JavaScript

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>";
Elcast
  • 59
  • 2
  • 8