ESP32 XML: Obtaining the value of an attribute

In this tutorial we will learn how to parse a XML document and obtain the value of an attribute in one of the elements. The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this tutorial we will learn how to parse a XML document and obtain the value of an attribute in one of the elements. We will be using the tinyxml2 library on the ESP32 and the Arduino core.

For a getting started tutorial on how to use this library on the ESP32, please check here.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

We will start the code by including the tinyxml2.h library and declaring the use of the tinyxml2 namespace.

#include <tinyxml2.h>

using namespace tinyxml2;

Followed by that we are going to define a string with a XML document. It will have a root element and a nested element with an attribute called att. This attribute will contain the value 10.

char * testDocument = "<root><element att=\"10\">test</element></root>";

Moving on to the Arduino setup, we will start by opening a serial connection, so we are able to output results from our program.

Serial.begin(115200);

Then we will declare an object of class XMLDocument and perform the parsing of our document.

if(xmlDocument.Parse(testDocument)!= XML_SUCCESS){
    Serial.println("Error parsing"); 
    return; 
};

If the parsing is successful, we will proceed to obtain the root node of the document. We can do that with a call to the FirstChild method on our XMLDocument.

XMLNode * root = xmlDocument.FirstChild();

Next we will obtain the child element of the root node, since it is the one that contains the attribute we want to obtain. This is done with a call to the FirstChildElement method on our root node, passing as input the name of the element we want to obtain.

XMLElement * element = root->FirstChildElement("element");

To obtain our attribute, we need to call the QueryIntAttribute method on our element. Note that we can use this method since our attribute is a number, which means the conversion of the attribute to an integer is possible.

As first input of the method we need to pass the name of the attribute and as second input a pointer to an integer that will store the result.

This method will return XML_SUCCESS in case the attribute could be successfully retrieved and converted to an integer. Otherwise, it will return XML_WRONG_ATTRIBUTE_TYPE if the conversion can’t be performed or XML_NO_ATTRIBUTE if the attribute doesn’t exist [1].

int attribute;

if(element->QueryIntAttribute("att", &attribute) != XML_SUCCESS){

   Serial.println("Could not obtain the attribute");
   return;
};

To finalize, we will print the value of the attribute.

Serial.println(attribute);

The final code can be seen below.

#include <tinyxml2.h>

using namespace tinyxml2;

char * testDocument = "<root><element att=\"10\">test</element></root>";

void setup() {
    
  Serial.begin(115200);
   
  XMLDocument xmlDocument;
 
  if(xmlDocument.Parse(testDocument)!= XML_SUCCESS){
    Serial.println("Error parsing"); 
    return; 
  };
 
  XMLNode * root = xmlDocument.FirstChild();
  XMLElement * element = root->FirstChildElement("element");

  int attribute;

  if(element->QueryIntAttribute("att", &attribute) != XML_SUCCESS){

      Serial.println("Could not obtain the attribute");
      return;
  };

  Serial.println(attribute);
 
}

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP32, using the Arduino IDE. Once the procedure finishes, open the serial monitor.

You should get an output similar to figure 1. As can be seen, the value 10 was printed, which corresponds to the value of the attribute in the original XML document.

Output of the program, showing the value of the XML attribute.
Figure 1 – Output of the program, showing the value of the XML attribute.

References

[1] http://leethomason.github.io/tinyxml2/classtinyxml2_1_1_x_m_l_element.html#a8a78bc1187c1c45ad89f2690eab567b1

Leave a Reply