kaouter berrahal
kaouter berrahal

Reputation: 7

Parse xml with python getting x,y values

i need to get x,y localisation from xml file

-<TwoDimensionSpatialCoordinate>

<coordinateIndex value="0"/>

<x value="302.6215607602997"/>

<y value="166.6285651861381"/>

</TwoDimensionSpatialCoordinate>
from xml.dom import minidom
doc = minidom.parse("1.631791322.58809740.14.834982.40440.3641459051.955.6373933.1920.xml")

"""doc.getElementsByTagName returns NodeList
coordinate = doc.getElementsByTagName("coordinateIndex")[0]
print(coordinate.firstChild.data)
"""

coordinate = doc.getElementsByTagName("coordinateIndex")
for coordinateIndex in coordinate:
        value = coordinateIndex.getAttribute("value")
coordinatex = doc.getElementsByTagName("x")
for x in coordinatex:
        valuex = x.getAttribute("value")        
coordinatey = doc.getElementsByTagName("y")
for y in coordinatey:
            valuey = y.getAttribute("value")
            print("value:%s, x:%s,y:%s" % (value, x , y))

so when i execute i get this result value:22, x:,y:

can Anyone help me please ?:(

Upvotes: 0

Views: 445

Answers (2)

AKX
AKX

Reputation: 169338

Using the ElementTree API (use ET.parse(filename).getroot() instead of ET.XML() to load from a file):

from xml.etree import ElementTree as ET

xml = ET.XML("""
<?xml version="1.0" ?>
<Things>
    <TwoDimensionSpatialCoordinate>
        <coordinateIndex value="0"/>
        <x value="302.6215607602997"/>
        <y value="166.6285651861381"/>
    </TwoDimensionSpatialCoordinate>
    <TwoDimensionSpatialCoordinate>
        <coordinateIndex value="1"/>
        <x value="3.6215607602997"/>
        <y value="1.6285651861381"/>
    </TwoDimensionSpatialCoordinate>
</Things>
""".strip())

coords_by_index = {}
for coord in xml.findall(".//TwoDimensionSpatialCoordinate"):
    coords_by_index[coord.find("coordinateIndex").get("value")] = (
        coord.find("x").get("value"),
        coord.find("y").get("value"),
    )
print(coords_by_index)

outputs

{
  '0': ('302.6215607602997', '166.6285651861381'),
  '1': ('3.6215607602997', '1.6285651861381'),
}

Upvotes: 0

shabelski89
shabelski89

Reputation: 19

As your example xml file

<?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_value, atr_x, atr_y in zip(values, coordX, coordY):
        value = atr_value.getAttribute('value')
        x = atr_x.getAttribute('value')
        y = atr_y.getAttribute('value')
        d[value] = [x, y]
    return d


result = main('/path/file.xml')
print(result)

# {'0': ['302.621', '166.628'], '1': ['3.621', '1.628']}

Upvotes: 1

Related Questions