How to create xml node with an attribute value using ofxxmlsettings?


#1

this is the code i am using :

int positionID = 0;
int i = 0;
	positions.addTag("position");
	positions.pushTag("position", positionID);
	positions.addAttribute("position", "type", "mouvement",positionID);

	for (vector<int>::iterator it1 = dynamixelsPosition.begin(); it1 != dynamixelsPosition.end(); ++it1) {
		string dynamixel = "dynamixel["+ std::to_string(i) +"]";
		
		positions.addValue(dynamixel, *it1);
		
		i++;
	}
	positions.popTag();//pop position
	positions.saveFile("positions.xml");
	positionID++;

i am getting this as a result :

<position>
    <dynamixel[0]>28091</dynamixel[0]>
    <dynamixel[1]>31300</dynamixel[1]>
    <dynamixel[2]>3676</dynamixel[2]>
    <dynamixel[3]>27943</dynamixel[3]>
</position>
<position>
    <dynamixel[0]>26365</dynamixel[0]>
    <dynamixel[1]>16519</dynamixel[1]>
    <dynamixel[2]>437</dynamixel[2]>
    <dynamixel[3]>11255</dynamixel[3]>
</position>

but i am trying to have something like this as a result :

<position type=mouvement>
    <dynamixel[0]>28091</dynamixel[0]>
    <dynamixel[1]>31300</dynamixel[1]>
    <dynamixel[2]>3676</dynamixel[2]>
    <dynamixel[3]>27943</dynamixel[3]>
</position>
<position type=mouvement>
    <dynamixel[0]>26365</dynamixel[0]>
    <dynamixel[1]>16519</dynamixel[1]>
    <dynamixel[2]>437</dynamixel[2]>
    <dynamixel[3]>11255</dynamixel[3]>
</position>

Thanks for your help


#2

Hi, which version of OF? I added an attribute this way:

using OF 0.10 and http://openframeworks.cc/documentation/utils/ofXml/ but I think you are using a different xml library.

Note: according to https://www.xmlvalidation.com/ you can’t have square brackets in your tag names.


#3

Thanks for your response but i am using OF 0.9 and i managed to do it this way :

`int numTag = myXml.addTag("positionForm");
	myXml.setAttribute("positionForm", "type", "move", numTag);
	myXml.pushTag("positionForm", numTag);
	for (vector<int>::iterator it1 = dynamixelsPosition.begin(); it1 != dynamixelsPosition.end(); ++it1) {
		string dynamixel = "dynamixel_" + std::to_string(i);
		int numtTagDyn = myXml.addTag("dynamixel");
		myXml.addAttribute("dynamixel", "id", i, numtTagDyn);
		myXml.addAttribute("dynamixel", "position", *it1, numtTagDyn);

		i++;
	}
	myXml.popTag();//pop position
	myXml.saveFile("myXml.xml");`