Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 XML Parsing - Find a specific text (ElementTree)
Hi all,

I have a problem to parse a specific text from a xml file.

What I need is the Value (BB001234) of the IDTAG but I didn't know how to grab them.

Here is my full .xml file and my python code.
The problem is that the quantity of the "<DataPoint> ### </DataPoint>" can change.

Hopefully someone can help me.

Thank you

import os
from xml.etree import ElementTree

file_name = 'cumulus.xml'
full_file = os.path.abspath(os.path.join('data', file_name))
dom = ElementTree.parse(full_file)

assy = dom.findall('WorkOrders/CumulusWorkOrder/Assembly')

for c in assy:
    item = c.find('PartNumber').text
    serial = c.find('SerialLotNumber').text
    desc = c.find('Description').text.encode('utf-8')
    # idtag = c.find('IDTAG').text

    #print(' * {} - {} - {} - {}'.format(
    #    item, serial, desc, idtag
    print(' * {} - {} - {} - '.format(
        item, serial, desc

$ python * 1234567 - 1234567.abcdef - Item Description -
You can use lxml:
from lxml import etree
import os

tree = etree.parse('cumulus.xml')
# print(etree.tostring(tree))
elementPath ='/CumulusWorkOrderGroup/WorkOrders/CumulusWorkOrder/Assembly/DataPoints/DataPoint/Value'
element = tree.xpath(elementPath)
If you play with is a bit, you can get a better path (it's the 22nd DataPoint), that's why the 22 index here:
Note that you can iterate though 'element' if you don't know what the index is:
for n, item in enumerate(element):
    print(f'{n}: {etree.tostring(item)}')
0: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 1: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 2: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 3: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 4: b'<Value xmlns:xsi="" xmlns:xsd="">No</Value>\n' 5: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 6: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 7: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 8: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 9: b'<Value xmlns:xsi="" xmlns:xsd="">Optiklot</Value>\n' 10: b'<Value xmlns:xsi="" xmlns:xsd="">No</Value>\n' 11: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 12: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 13: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 14: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 15: b'<Value xmlns:xsi="" xmlns:xsd="">No</Value>\n' 16: b'<Value xmlns:xsi="" xmlns:xsd="">No</Value>\n' 17: b'<Value xmlns:xsi="" xmlns:xsd="">No</Value>\n' 18: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 19: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 20: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 21: b'<Value xmlns:xsi="" xmlns:xsd=""/>\n' 22: b'<Value xmlns:xsi="" xmlns:xsd="">BB001234</Value>\n' 23: b'<Value xmlns:xsi="" xmlns:xsd="">True</Value>\n'
The parsers in stand library is not the best,better of using lxml as Larz60+ show or BeautifulSoup with lxml as chosen parser.
from bs4 import BeautifulSoup

soup = BeautifulSoup(open("cumulus.xml"), 'lxml')
id_tag = soup.find("measurement", string="IDTAG")
Thanks to both of you!
This is really helpfull.

Best Regards

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  ElementTree kkrish 2 200 Apr-27-2019, 01:36 AM
Last Post: kkrish
  Parsing infor from scraped files. Larz60+ 2 212 Apr-12-2019, 05:06 PM
Last Post: Larz60+
  Fetching and Parsing XML Data FalseFact 3 249 Apr-01-2019, 10:21 AM
Last Post: Larz60+
  [split] How to find a specific word in a webpage and How to count it. marpop 2 233 Mar-12-2019, 08:25 AM
Last Post: snippsat
  Python + request from specific website - please help hoff1022 8 433 Feb-14-2019, 06:52 PM
Last Post: buran
  .txt return specific lines or strings s_o_what 8 351 Feb-08-2019, 11:49 AM
Last Post: snippsat
  Selenium Parsing (unable to Parse page after loading) oneclick 6 487 Jan-13-2019, 03:10 AM
Last Post: oneclick
  BeautifulSoup4, How to get an HTML tag with specific class. Broadsworde 6 510 Nov-22-2018, 05:25 PM
Last Post: snippsat
  XML parsing from URL mightyn00b 5 1,785 Nov-22-2018, 02:59 AM
Last Post: Larz60+
  How to find particular text from td tag using bs4 Prince_Bhatia 7 585 Sep-24-2018, 08:36 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)