In this video demonstration, you will be able to learn how to create an XML document and print it to the console as well as write it to a file using a module called ElementTree. To begin with the process of creating an XML document, you need to make sure that you have the PyDev Plugin installed on Eclipse. If you are not sure that you have this plugin, look at the menu bar and if you don’t see an option called ‘Pydev’ on it, then it means that you do not have this plugin. If you don’t have it, all you need to do is go to help Eclipse Marketplace and search for PyDev. You can then install the plugin directly from the Eclipse Marketplace and you should be ready to go.
Once you have the PyDev plugin installed on Eclipse, you will be able to create a PyDev project from the file menu. If you don’t see any option for the PyDev Project when you point to ‘New’, then you will probably find it in another section where you can locate it under PyDev. Click on the option that says PyDev to open it. Next you will have to give the project a suitable name. Once done, hit finish. After this, you will need to create a new Python Module. You will have to provide a package name and package as per your choice. Again you will need to hit finish to create the module.
After going thorough all of these initial settings, you will be within the module that you have created. The first thing you will have to do now is to start off with your imports. The imports are done as follows:
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
This importing format will make sure that you do not have to fully qualify ElementTree or Element. This will be saving you from a lot of additional typing work. You can also add an addition to the above mentioned code as,
import xml.etree.ElementTree as etree
By doing this, you will be giving the whole import an assumed name as etree.
When building an XML document, you need to make sure that you have a root element. For demonstration purposes, the XML document that would be built is as follows,
<person id=’123′>
<name>Julie</name>
</person>
The root tag here is person, the root has an attribute called as id of value 123. The person tag also has a child tag known as name and this tag has a text node called Julie. The first thing you need to do is to determine whether the person tag is your root. You can do this as follows,
root =Element (‘person’)
The word Element used here as a constructor, it refers to a tag. Next you will need to build your tree object. This can be done by adding,
tree=ElementTree(root)
By doing this, you would have created the most basic type of XML document. This would look like,
<person />
The aim here is to build this document a little bit further. But before you do that, it can be a good idea to take a look at what you’ve got so far. You can do this with the following command.
Print etree.tostring(root)
When you run this with Python Run, you should be able to find <person /> as the output. If you do find this result, you can build the file further by creating a tag for name. This can be done as follows,
name=Element(‘name’)
root.append(name)
This will make name as a child tag of person. Run the code so far and you should be able to find <person><name/></person> in the output. Now that you have a variable by which you can add reference name, you can say name.text=’Julie’ and then run the code once more, you should get,
<person><name>Julie</name></person> as the result. The last thing you need to do here to complete the XML file is to give it an attribute. This will complete the creation of an XML file. The way you set the attribute is to decide which tag you are working with. In this case it is a root tag where the attribute called ‘id’ should be present in the XML file. This is done as follows,
root.set(‘id’, ‘123’)
Here the key will be id and the value that is being passed is 123. When you run this code you should be able to get <person id=”123”><name>Julie</name></person> which is the desired XML file. You can now save this to the file. The way to do that is as follows,
tree.write(open(‘person.xml’, ‘w’)) Here the file name could be anything as per your choice. This will make sure that the code is being written to the given file name and the ‘w’ will make sure that the file will be writable. This will be relative to the code present here. If you want a more absolute path, you will need to write the file as tree.write(open(r ‘c:\person.xml‘, ‘w’)) This will write the file directly into the C drive. You could also give a more detailed path if you wish the file to be at a certain folder. Once you run it, you will be able to find it in the directory that you have chosen. Make sure to open the file just to make sure that everything is all right.
Please Share This Knowledge With Others!