Odoo 14 上传xml文件

问题描述

在我的模块中,我需要上传和读取 xml 数据文件,但我找不到任何示例。在我的表单上,我添加文件并需要通过单击按钮来解析数据。我的代码: 模型.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"/>

我知道我的 parse_data_from_xml 函数不正确,但我找不到任何示例。 数据xml文件是这样的:

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

解决方法

我找到了一个答案,前 3 行没问题,但其他几行就没有想要的那么好。也许有人知道更好的方法。

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)