简体   繁体   中英

Extracting all attribute and tag along with line number from an XML file in python

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 eg <something>Value<something> , and I want to know all <something> with their line numbers. Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM