Skip to main content

XML parsing and Python: the lxml way

Today I was tinkering with an idea involving parsing a Html file by taking an XML structure and building a simple xquery engine in python.

I know...most of my "World changing ideas" finally end up on my unfinished/fragile Github Project category.
But knowing how it's gonna help me in long drive I tried to give it a shot anyway.

And this is when I discovered lxml and immediately fell in love with it.
But rather going to the intricacies I'm gonna share how simple it was for me to pars an xml file with it.

First, we'll make sure we have everything in place.
Things we need
  • lxml
Yeah...that's kinda only thing you'll need :) (and of course Python!!!).
If you are sing Ubuntu or Xubuntu (like me) then just goto synaptics package manager and install it. Or get it from the website if you are gonna tinker in windows.

Now, let's assume that we want to parse a xml file named 'file.xml' with the following content:



Now I want to get all the element_id for my purpose from the file. The following python code serves the purpose:


from lxml import etree

doc = etree.parse('file.xml')
element_list = doc.findall('element')

for store in store_list:
element_id = element.findtext('element_id')


print element_id


Another way would be  


from lxml import etree

doc = etree.parse('file.xml')

for element in doc.getiterator('element'):
element_id = element.findtext('element_id')
print element_id

Comments

Popular posts from this blog

ARCore and Arkit, What is under the hood: SLAM (Part 2)

In our last blog post ( part 1 ), we took a look at how algorithms detect keypoints in camera images. These form the basis of our world tracking and environment recognition. But for Mixed Reality, that alone is not enough. We have to be able to calculate the 3d position in the real world. It is often calculated by the spatial distance between itself and multiple keypoints. This is often called Simultaneous Localization and Mapping (SLAM). And this is what is responsible for all the world tracking we see in ARCore/ARKit. What we will cover today: How ARCore and ARKit does it's SLAM/Visual Inertia Odometry Can we D.I.Y our own SLAM with reasonable accuracy to understand the process better Sensing the world: as a computer When we start any augmented reality application in mobile or elsewhere, the first thing it tries to do is to detect a plane. When you first start any MR app in ARKit, ARCore, the system doesn't know anything about the surroundings. It starts pro

ARCore and Arkit: What is under the hood : Anchors and World Mapping (Part 1)

Reading Time: 7 MIn Some of you know I have been recently experimenting a bit more with WebXR than a WebVR and when we talk about mobile Mixed Reality, ARkit and ARCore is something which plays a pivotal role to map and understand the environment inside our applications. I am planning to write a series of blog posts on how you can start developing WebXR applications now and play with them starting with the basics and then going on to using different features of it. But before that, I planned to pen down this series of how actually the "world mapping" works in arcore and arkit. So that we have a better understanding of the Mixed Reality capabilities of the devices we will be working with. Mapping: feature detection and anchors Creating apps that work seamlessly with arcore/kit requires a little bit of knowledge about the algorithms that work in the back and that involves knowing about Anchors. What are anchors: Anchors are your virtual markers in the real wo

Road to the Grant : Immersive Payment

  This is a news close to my heart. My project "Immersive Payment" has been awarded with Grant for the Web , and I will be focusing on using Web Monetization to enable Micro Payment and for Web Mixed Reality assets, as well as for 3d contents. It took me almost five months from learning about the initiative to actually getting thee Grant. It all started back when in Mozilla TechSpeaker call a fellow TechSpeaker and a friend Andrzej Mazur explained bout the program. He himself is also an early awardee . After the talk, I got really excited and interested in the potential and concept of Web Monetization, however, it wasn't until June that I really decided to apply. The application process was a bit of roller coaster ride for me. I had an idea of what I wanted to do and also explored what is possible right now. But a bit of termoil in my personal life kept me from going beyond toy implementations. Fast forward to June end and I finally had the project chalked out with init