Guest User

Python XML parsing

a guest
May 15th, 2015
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. xmlstr = r"""<?xml version="1.0"?>
  2. <LandXML xmlns="http://www.landxml.org/schema/LandXML-1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.landxml.org/schema/LandXML-1.2 http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd" date="2015-05-15" time="09:47:43" version="1.2" language="English" readOnly="false">
  3.    <Units>
  4.        <Metric areaUnit="squareMeter" linearUnit="meter" volumeUnit="cubicMeter" temperatureUnit="celsius" pressureUnit="milliBars" diameterUnit="millimeter" angularUnit="decimal degrees" directionUnit="decimal degrees"></Metric>
  5.    </Units>
  6.    <Project name="C:\Users\Rade\Downloads\Situacija i profili.dwg"></Project>
  7.    <Application name="AutoCAD Civil 3D" desc="Civil 3D" manufacturer="Autodesk, Inc." version="2014" manufacturerURL="www.autodesk.com/civil" timeStamp="2015-05-15T09:47:43"></Application>
  8.    <Alignments name="">
  9.        <Alignment name="Proba" length="1201.057158008475" staStart="0." desc="">
  10.            <CoordGeom>
  11.                <Line dir="10.014571204947" length="209.285340662374">
  12.                    <Start>776.719431311241 -399.949629732524</Start>
  13.                    <End>813.113864060552 -193.853052659974</End>
  14.                </Line>
  15.                <Spiral length="435.309621307305" radiusEnd="300." radiusStart="INF" rot="cw" spiType="clothoid" theta="41.569006803911" totalY="101.382259815422" totalX="412.947724836996" tanLong="298.633648469722" tanShort="152.794210168398">
  16.                    <Start>813.113864060552 -193.853052659974</Start>
  17.                    <PI>865.04584458778 100.230482065888</PI>
  18.                    <End>785.087350093002 230.433054310499</End>
  19.                </Spiral>
  20.                <Curve rot="cw" chord="150.078507004323" crvType="arc" delta="28.970510103309" dirEnd="299.475054297727" dirStart="328.445564401036" external="9.849481983234" length="151.689236185509" midOrd="9.536387074322" radius="300." tangent="77.502912753511">
  21.                    <Start>785.087350093002 230.433054310499</Start>
  22.                    <Center>529.44434090873 73.440532656728</Center>
  23.                    <End>677.05771309169 334.61153478517</End>
  24.                    <PI>744.529424397382 296.476647100012</PI>
  25.                </Curve>
  26.                <Spiral length="127.409639008589" radiusEnd="INF" radiusStart="300." rot="cw" spiType="clothoid" theta="12.166724307463" totalY="8.989447716697" totalX="126.8363181841" tanLong="85.141254974713" tanShort="42.653117896421">
  27.                    <Start>677.05771309169 334.61153478517</Start>
  28.                    <PI>639.925187941987 355.598770007863</PI>
  29.                    <End>558.639337133827 380.929458057393</End>
  30.                </Spiral>
  31.                <Line dir="287.308329990254" length="277.363320844698">
  32.                    <Start>558.639337133827 380.929458057393</Start>
  33.                    <End>293.835705515579 463.448840215686</End>
  34.                </Line>
  35.            </CoordGeom>
  36.            <Profile name="Proba">
  37.                <ProfAlign name="Niveleta">
  38.                    <PVI>0. 329.48636525895</PVI>
  39.                    <CircCurve length="69.993187715052" radius="5000.">512.581836381869 330.511528931714</CircCurve>
  40.                    <CircCurve length="39.994027682446" radius="5000.">948.834372016349 337.491569501865</CircCurve>
  41.                    <PVI>1201.057158008475 339.509351789802</PVI>
  42.                </ProfAlign>
  43.            </Profile>
  44.        </Alignment>
  45.    </Alignments>
  46. </LandXML>"""
  47.  
  48. import xml.etree.ElementTree as ET, functools, contextlib, pprint
  49. qname = functools.partial(ET.QName, "http://www.landxml.org/schema/LandXML-1.2")
  50. root = ET.fromstring(xmlstr)
  51. lists = {}
  52.  
  53. def process(part):
  54.     def candidates():
  55.         yield from part.attrib.values()
  56.         for child in part:
  57.             yield from child.text.split()
  58.     for candidate in candidates():
  59.         with contextlib.suppress(ValueError):
  60.             yield float(candidate)
  61.  
  62. for type in "Line Spiral Curve".split():
  63.     lists[type] = current = []
  64.     for part in root.iter(qname(type)):
  65.         current.append(list(process(part)))
  66.  
  67. pprint.pprint(lists, compact=True)
Advertisement
Add Comment
Please, Sign In to add comment