简体   繁体   中英

Get coordinate(x,y) from xml file and put it into float list

I want to put several coordinates (x, y) recovered from an xml file in a list that I can used with a drawcontour or polyline function the problem is that I don't know how to put them in a list I used liste.append but its not working :( please help me

<?xml version="1.0" ?>
<TwoDimensionSpatialCoordinate>
    <coordinateIndex value="0"/>
        <x value="302.6215607602997"/>
        <y value="166.6285651861381"/>
    <coordinateIndex value="1"/>
        <x value="3.6215607602997"/>
        <y value="1.6285651861381"/>
</TwoDimensionSpatialCoordinate>
import xml.dom.minidom


def main(file):
    doc = xml.dom.minidom.parse(file)
    values = doc.getElementsByTagName("coordinateIndex")
    coordX = doc.getElementsByTagName("x")
    coordY = doc.getElementsByTagName("y")
    d = []
    for  atr_x in  coordX:
        for   atr_y in  coordY:
            x = atr_x.getAttribute('value')
            y = atr_y.getAttribute('value')
            print("x",x,"y",y)
    d.append(x)
    d.append(y)
    print(d)


result = main('1.631791322.58809740.14.834982.40440.3641459051.955.6373933.1920.xml')
print(result)

Output:

x 302.6215607602997 y 179.53418754193044
x 317.14038591056607 y 179.53418754193044
x 328.11016491298955 y 179.53418754193044
x 337.6280614003864 y 179.53418754193044
x 350.0497229178365 y 179.53418754193044
x 363.9232669503133 y 179.53418754193044

This result is when I get the x,y coordination from xml file but when I add d.append it doesn't define the d : NameError: name 'd' is not defined .

  1. Your XML is strange ( x and y are not in coordinateIndex )
  2. Indentation matters in python
  3. You probably want to try ElementTree, which is considered a better alternative to minidom
  4. Working code for minidom and your input format
def main(file):
    doc = xml.dom.minidom.parse(file)
    coordX = doc.getElementsByTagName("x")
    coordY = doc.getElementsByTagName("y")
    d = []
    for atr_x, atr_y in zip(coordX, coordY):
        x = atr_x.getAttribute('value')
        y = atr_y.getAttribute('value')
        print("x", x, "y", y)
        d.append(x)
        d.append(y)
    return d

it's work now :D i put the code in case someone need it later

import xml.dom.minidom
import cv2 
import numpy as np
import matplotlib.pyplot as plt



def main(file):
    doc = xml.dom.minidom.parse(file)
    coordX = doc.getElementsByTagName("x")
    coordY = doc.getElementsByTagName("y")
    d = []
    for atr_x, atr_y in zip(coordX, coordY):
        x = atr_x.getAttribute('value')
        y = atr_y.getAttribute('value')
        #print("x", x, "y", y)
        d.append(x)
        d.append(y)

    a = map(float, d)
    print(tuple(zip(a, a)))

    return a  

result = main('1.631791322.58809740.14.834982.40440.3641459051.955.6373933.1920.xml')
print(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM