0

What would be a good way of returning the known Min and Max values from the xml below using python's import xml.etree.ElementTree as ET

I'm struggling to find a clean way of getting the Max and Min values, so I'm open to any suggestions. I'm not in control of the XML creation.

Here is a snippet of my xml.

<options>
    <option>
        <name v="clamp"/>
        <value v="0"/>
    </option>
    <option>
        <name v="default"/>
        <value v="10"/>
    </option>
    <option>
        <name v="max"/>
        <value v="10"/>
    </option>
    <option>
        <name v="min"/>
        <value v="0"/>
    </option>
    <option>
        <name v="step"/>
        <value v="1"/>
    </option>
</options>

2 Answers 2

2

Basically, you want to check attribute of element name if it is equal to min or max. If so, you can get the value from value element by access though attribute and get value from key v.

Here is the snippet to find min and max value from the given string (named as text) using lxml library.

from lxml import etree

tree = etree.fromstring(text) # or etree.parse if you want to parse XML file instead

for e in tree.xpath('//option'):
    if e.find('name').attrib.get('v') == 'min':
        min_val = e.find('value').attrib.get('v')
    if e.find('name').attrib.get('v') == 'max':
        max_val = e.find('value').attrib.get('v')

print(min_val, max_val) # ('0', '10')

note that you might have to write if/else statement if the attribute does not exist. In that case, it can return None which gives the error.

Sign up to request clarification or add additional context in comments.

Comments

0

As an option, you could use xml.etree.ElementTree.XMLParser if your xml is already read as string:

from xml.etree.ElementTree import XMLParser

class Parser:                     # The target object of the parser

  min = 0
  max = 0
  store_max = False
  store_min = False

  def start(self, tag, attrib):   # Called for each opening tag.

    if self.store_max:
      self.max = attrib.get('v')
      self.store_max = False

    if self.store_min:
      self.min = attrib.get('v')
      self.store_min = False

    if attrib.get('v') == 'max':
      self.store_max = True
    if attrib.get('v') == 'min':
      self.store_min = True

  def end(self, tag):             # Called for each closing tag.
    pass                          # We do not need to do anything when tag is closing.

  def data(self, data):
    pass                          # We do not need to do anything with data.

  def close(self):    # Called when all data has been parsed.
    return self.min, self.max


xml = """<options>
    <option>
        <name v="clamp"/>
        <value v="0"/>
    </option>
    <option>
        <name v="default"/>
        <value v="10"/>
    </option>
    <option>
        <name v="max"/>
        <value v="10"/>
    </option>
    <option>
        <name v="min"/>
        <value v="0"/>
    </option>
    <option>
        <name v="step"/>
        <value v="1"/>
    </option>
</options>"""

target = Parser()
parser = XMLParser(target=target)
parser.feed(xml)
print(parser.close())

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.