0

So I have a list of tag, and I want to check how many of these tags are there in XML file also, and I want to get the respective line number so that I can mark those lines and show them on my UI side. I went to read the documentation of lxml but I couldn't understand it well and I am still stuck on the problem, I am not able to even start the code.

Take this for example:

<domain type='kmc' id='007'>
  <name>virtual bug</name>
  <uuid>66523dfdf555dfd</uuid>
  <os>
    <type>hvm</type>
    <boot>Windows</boot>
  </os>
  <memory unit='KiB'>524288</memory>
  <currentMemory unit='KiB'>270336</currentMemory>
  <vcpu placement='static'>10</vcpu>

Let's say I want to search for

['name','boot']

I want to process it line by line. How do I do this ? I am interested in tag values e.g <something>Value<something>, and I want to know all <something> with their line numbers. Thanks

Saurav Ahlawat
  • 81
  • 1
  • 4
  • 9
  • Does this answer your question? [How do I extract value of XML attribute in Python?](https://stackoverflow.com/questions/48746478/how-do-i-extract-value-of-xml-attribute-in-python) – dt170 May 14 '20 at 09:36
  • No sorry this doesn't. My question is different, I want to get the tage values , not attributes – Saurav Ahlawat May 14 '20 at 09:38

1 Answers1

0

We can't write the algorithm for you to perform this task, that's not what SO is for, but we can guide you to how to implement it.

You can use ElementTree and parse line by line the XML, when you find the line you are looking for you can act on it.

For the line numbers, I would create a counter starting at 0 and increase it every time you get a new line in child

import xml.etree.ElementTree as ET
tree = ET.parse('myfile.xml')
root = tree.getroot()

counter = 0;

for child in root:
    print(child.tag, child.attrib)
    counter++

    if child.tag == "name" 
        print(counter)

You can find more about it here:

https://www.datacamp.com/community/tutorials/python-xml-elementtree

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • I have already read this tutorial, it gives me all tag, but is there any way to get the corresponding line numbers? – Saurav Ahlawat May 14 '20 at 09:52
  • This solution doesn't actually solves the problem. It doesn't provide the child of child and so on, this just works for the direct child of root, and line number logic is also wrong I guess, cause it is giving me wrong answer. – Saurav Ahlawat May 14 '20 at 10:59