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 {}

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

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.