0

in my module I need to upload and read xml data file and I can,t find any examples for this. On my form I add file and need to parse data by clicing button. My code: model.py

file = fields.Binary(string='File', attachment=True, store=True)
file_name = fields.Char('File name')

def parse_data_from_xml(self):
  if self.file:
    data = etree.parse(self.file)

.xml

<field name="file_name" string="File name"/>
<field name="file" string="Upload xml file" filename="file_name"/>
<button name="parse_data_from_xml" string="Upload data from file" type="object" class="oe_highlight"/>

I understand that my parse_data_from_xml function isn't correct, but I can't find any examples. Data xml file is something like this:

<InfoPart>
    <MetricInfo>
      <CoordinateSystem>
        <SC63>
          <X></X>
        </SC63>
      </CoordinateSystem>
      <MeasurementUnit>
        <M>га</M>
      </MeasurementUnit>
      <PointInfo>
        <Point>
          <UIDP>1</UIDP>
          <PN>1001</PN>
          <DeterminationMethod>
            <Survey></Survey>
          </DeterminationMethod>
          <X>5403910.077</X>
          <Y>6379655.092</Y>
          <MX>0.05</MX>
          <MY>0.05</MY>
        </Point>
Umks
  • 55
  • 2
  • 8

1 Answers1

0

I found one answer, firs 3 lines is ok, but the other lines is not so nice as wanted. Maybe somone know better method.

stream = BytesIO(base64.b64decode(self.file))
root = ET.parse(stream).getroot()
points = []
for elem in root:
  for info in elem.findall('MetricInfo'):
    for minfo in info.findall('PointInfo'):
      for pinfo in minfo.findall('Point'):
        for pdata in pinfo:
          if pdata.tag == 'UIDP':
            p_seq = int(pdata.text)
Umks
  • 55
  • 2
  • 8