ESP32 XML: navigating through a list of elements

In this tutorial we will learn how to parse a XML document and navigate through a list of elements, printing their values. We will be testing this tutorial on the ESP32, using the Arduino core and the TinyXML-2 library. 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 navigate through a list of elements, printing their values.

We will be testing this tutorial on the ESP32, using the Arduino core and the TinyXML-2 library. For an introductory tutorial and installation instructions for this library to be used with the Arduino core, 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. Followed by that, we will declare the using of the tinyxml2 namespace.

#include <tinyxml2.h>

using namespace tinyxml2;

Then we will define a string containing a XML document. This document will have a root element and a nested list of elements. Each element of the list will contain an integer.

char * testDocument = "<root><element>7</element><element>4</element><element>10</element></root>";

Moving on to the setup function, we will start by opening a serial connection, so we can output the results of our program.

Serial.begin(115200);

Then we will declare an object of class XMLDocument, which will allow us to parse the XML document.

XMLDocument xmlDocument;

To perform the actual parsing, we simply need to call the parse method on the previous object, passing as input the string with the XML document.

We will also check if the result of this method call is equal to XML_SUCCESS, to confirm no errors occurred in the parsing procedure.

XMLDocument xmlDocument;

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

To start navigating the XML document, we will first obtain the root node with a call to the FirstChild method on the XMLDocument object. This method call will return a pointer to an object of class XMLNode.

XMLNode * root = xmlDocument.FirstChild();

Now that we have the root node, we will obtain its first child element with a call to the FirstChildElement method. As input, we will pass a string with the name of the element that we want to look for.

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

Note that the previous call will return the first element named “element“. Nonetheless, since we have a list of them, we will iterate through this list on a loop.

As we will see in the loop implementation, we will iterate through the elements until there are no more elements available, which means our XMLElement pointer will be equal to NULL.

while(element != NULL){
   // loop implementation
}

Before we analyze how to navigate through the elements, we will first print the value of the current element.

int val;
element->QueryIntText(&val);
Serial.println(val);

Then, to obtain the next element, we simply need to call the NextSiblingElement method on the current element.

If there is a sibling element, the method will return a pointer to a XMLElement. Otherwise, it will return NULL and thus the loop will finish.

The full loop is shown below.

while(element != NULL){

    int val;
    element->QueryIntText(&val);
    Serial.println(val);

    element = element->NextSiblingElement("element");
}

The final code can be seen below.

#include <tinyxml2.h>

using namespace tinyxml2;

char * testDocument = "<root><element>7</element><element>4</element><element>10</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");

  while(element != NULL){

    int val;
    element->QueryIntText(&val);
    Serial.println(val);

    element = element->NextSiblingElement("element");
  }
}

void loop() {}

Testing the code

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

You should obtain a result similar to figure 1. As can be seen, we have obtained the values of each element of the list, as expected.

Output of the program, showing the values of the elements of the list.
Figure 1 – Output of the program, showing the values of the elements of the list.

Leave a Reply