Definition of Rust XML
XML is very different from JSON and if we look closely it is very difficult to handle. XML has full form which is extensible markup language, its main purpose is to transfer data, store data and etc. To create XML file we have to follow some standard define by them otherwise it generates error for us. In XML we create tags to store a represent our data in file. In rust we have so many different libraries to deal with XML, there is no in built support for this, in order to use XML in our program we have include dependency in our configuration file to make it work. In the coming section, we will discuss more the different library in detail, how to use and implement XML in rust programming language for better understanding, and its usage for beginners.
Syntax:
As we know that rust does not have any inbuilt library for XML, we have to use external library for make this work. We can read an XML file also we can create the XML content using this library, let’s take an example how it looks for better understanding see below;
1) To read :
let variable_name = EventReader::new(your_XMl_file);
As you can see in the above lines of syntax we are trying to read the XML content from XML file, for this, we have to use EventReader from crate library, which needs to be include in our configuration file. Let’s take a practice syntax for beginners to understand it better see below;
e.g. :
let demo = EventReader::new(demo.xml);
In the coming section of the tutorial, we will discuss more the XML in detail how it works internally and what’s steps we need to follow to configure this in rust programming language.
How XML works in Rust?
As of now we already know that XML is also used to store, represent and transfer data just like Json but XML’s are much are complicated then JSON. Inside XML file we cannot store every type of data because at some point it cannot parse it. In rust, we have so many different library available which is used to deal with XML in rust. Let’s discuss each of them in detail to understand it better see below;
1) Crate XML data: This library provides support to handle XML data in rust, by the use of this we can parse our data, cerate XML data, and so on. To parse the XML data by using this library, we have one advantage it provides us one generic interface which will help us to parse it. User can be able to define the parsing and serialization for their XML data.
2) Crate simple XML builder: We have one more library which is very easy to use and implement, this is calls as crate simple XML builder. By the use of this, we can easily create the XML file and add its attribute in it. To create XML tags we have to use one class for that called as XMLElement from the simple XML builder library. In this section we will see its syntax how to use this in rust programming for better understanding see below;
Follow below steps to create it:
1) First we have to include the library inside the program in order to use it, otherwise program will not compile and run.
e.g. :
use std::fs::File;
use simple_xml_builder::XMLElement;
These two imports are necessary first one is used to handle the file operations and second one is from the simple_xml_builder library which is responsible to deal with the XML in rust.
2) So in the second step we will create one file which will store the XML data for us. To handle the file operation, we have read include the File from rust itself, so in order to create it we have to follow the below lines of code see below;
e.g. :
let mut variable_name = File::create("youfile.xml")?;
As you can see in the above lines of code we are trying to create one file here, inside this you can pass your file name with .xml extension.
3) In this step we can create the XMLElement that will represent our data into the XML file we have already created. For this, we will use XMLElement from the simple_xml_builder library, keep in mind you have imported this at correct place to avoid error. Below see the lines of code to create it ;
e.g. :
let mut variabel_name = XMLElement::new("element_name");
practise example : let mut emp = XMLElement::new("employee");
As you can see in the above lines of code we are trying to create an XML Element named as ’employee’ here. This will hold the data for employee object. after this, we can add its value by using the attribute property of XMLElement.
4) After return on of XMLElement we can now add attribute for it, XMLElement support various methods which can be used on XMLElement to add their respective values. Below see the lines of code for better understanding ;
e.g.:
xmlelement.add_attribute("key", "value");
practise line of code :employee.add_attribute("roll", "001");
As you can see in the above lines of code we are adding values to the employee element her, for this we are using add_attribute() method of XMLElement, inside this method we can pass the value of our attribute in the form of key-value pair. In our case, we have cerate roll for employee element.
5) In this way we can create several XMLElement and add attribute of each of them. There is no restriction for this.
6) At the end we will write our XMLElement content to the file that we have created in the first step of the lines of code. Below see the lines of code to do this;
e.g. :.
xmlelement.write(file_object)?;
As you can see in the above lines of code we are trying to write to the file object here. For this we have to follow the syntax, to write in the fie w will call the write() method which is available in the XMLElement object, and inside this method we can pass our file object in which we really want to write, but remember this file should be of .xml extension, to avoid errors.
Examples
1) In this example we are creating XML file with some employee data inside it. This example will create the file at the specified location and write the XML content inside it. I will attach the file and also the snippet of the output for reference. This is a sample example for beginners to understand its implementation.
Example:
Code:
use std::fs::File;
use simple_xml_builder::XMLElement;
let mut demoXMlFile = File::create("C:\project\article\demo.xml")?; // please mentioned your file path here ..
let mut employee = XMLElement::new("employee");
employee.add_attribute("roll", "001");
let mut city = XMLElement::new("city");
city.add_text("Mumbai");
employee.add_child(city);
let mut age = XMLElement::new("salary");
salary.add_text("10000");
employee.add_child(salary);
let department = XMLElement::new("department");
department.add_text("IT");
employee.add_child(department);
let technology = XMLElement::new("technology");
technology.add_text("JAVA");
employee.add_child(technology);
let experience = XMLElement::new("experience");
experience.add_text("10");
employee.add_child(experience);
employee.write(demoXMlFile)?;
Output:
Conclusion
To use and del with XML in rust we have to use an internal library for this, we have so many options available for this. XML is a little bit difficult then JSON, but by using this library we can easily handle this in our rust program. XML more often used for transfer and store of data.
Recommended Articles
This is a guide to Rust XML. Here we discuss the definition, syntax, How XML works in Rust? and examples with code implementation. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses