0

I need to extract specific data from an xml file. The three data that I want is the node name Pause Code, Diecut and Blackout and their respective data.

The part that i want to extract data is this

  <Labels>
    <Label>
      <Measurement Name="Pause Code" Status="Pass" Failed="false">
        <Data>102000</Data>
      </Measurement>
      <Measurement Name="Diecut" Status="Pass" Failed="false">
        <Data>Pass (7.57,12.10mm)</Data>
      </Measurement>
      <Measurement Name="Blackout" Status="Pass" Failed="false">
        <Data>1244</Data>
      </Measurement>
      <errors />
      <ImageFileName />
    </Label>

The code that I am using is this

import xml.etree.ElementTree as ET
tree =ET.parse('006091_02_Harry_Convert.xml')
root = tree.getroot()
root.tag

for product in root:
    print(product.tag, product.attrib)


for child in product:
    print(child.tag,  child.attrib)


when I run the program I am getting this result but i can not see the data inside the Label . Any help pls.

okCount {}
checkCount {}
LabelCount {}
ReelInfo {}
Traverse {}
YearOfManuf {}
DaysSinceXmasManuf {}
ConcealmentThresholdReached {}
ReelStatus {}
MachineID {}
ModeInfo {}
CameraID {}
Paused {}
IsMaster {}
IsSlave {}
Labels {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}
Label {}

  • It might be an indentation issue, as I guess you might want to iterate over the children of each product. – abc Dec 19 '20 at 21:34

2 Answers2

1

This code will print the data as well:

import xml.etree.ElementTree as ET

tree = ET.parse('006091_02_Harry_Convert.xml')
root = tree.getroot()

for label in root:
    print(label.tag, label.attrib)

    for measurement in label:
        print(measurement.tag, measurement.attrib)
        for data in measurement:
            print(data.text)

        # or, if there is always exactly one data element
        print(measurement[0].text)

Two changes:

  • To get the data, you have to iterate over the measurements
  • If you want the text of something, you'll have to use .text

To print the name and the data text of each measurement, I would do this:

measurements = root.findall("./Label/Measurement")
for measurement in measurements:
    print(measurement.attrib["Name"], measurement[0].text)
anonymus1994.1
  • 308
  • 1
  • 6
0

please see this [link]1.

I think your code must such as this:

import xml.etree.ElementTree as ET
tree =ET.parse('006091_02_Harry_Convert.xml')
root = tree.getroot()
root.tag

for product in root:
    print(product.tag, product.attrib)


    for child in product:
        print(child.tag,  child.attrib)

please see this [link]2 too.

Darwin
  • 1,695
  • 1
  • 19
  • 29