Python 有三种方法解析 XML : SAX,DOM,以及 ElementTree,本博客主要是讲解DOM和ElementTree
DOM解析方法:
xml文件解析首先将xml文件加载进内存,然后读取文件中的内容.在内存将文件以树的结构进行保存,树根在上,树枝在下,整个xml文件被封装为Document对象,文件中的标签节点被封装为Node对象,标签节点中保存但数据被封装为Text对象
Document:表示整个文件对象
Node:表示标签对象
Text:表示标签中数据的内容
下面的讲解内容都是在下面这个xml文件中操作:
<?xml version="1.0" encoding="utf-8"?>
<root>
<user Id="1" Name="xiazheng">
<test id_menu="0" name_menu="auto" date="2023_3_14" time="16_44_15">
<item Time_Geo1="72.26271" Type_Geo1="Capsule" Time_Geo2="75.15987" Type_Geo2="Capsule" H_Geo="0.6757858" Time_DrawDone="76.96201" H_Line="0.6757858" />
<item Time_Geo1="77.52725" Type_Geo1="Cube" Time_Geo2="78.5939" Type_Geo2="Cube" H_Geo="0.7748006" Time_DrawDone="80.09358" H_Line="0.7748006" />
<item Time_Geo1="80.66089" Type_Geo1="Sphere" Time_Geo2="82.32736" Type_Geo2="Sphere" H_Geo="0.4040862" Time_DrawDone="84.02656" H_Line="0.4040862" />
<item Time_Geo1="84.56068" Type_Geo1="Capsule" Time_Geo2="86.4759" Type_Geo2="Capsule" H_Geo="0.9475007" Time_DrawDone="88.61422" H_Line="0.9475007" />
</test>
<test id_menu="1" name_menu="pinch" date="2023_3_14" time="16_44_41">
<item Time_Geo1="98.64845" Type_Geo1="Cube" Time_Geo2="106.5067" Type_Geo2="Cube" H_Geo="0.5008803" Time_DrawDone="115.2941" H_Line="0.5524983" />
<item Time_Geo1="115.8283" Type_Geo1="Capsule" Time_Geo2="123.8263" Type_Geo2="Capsule" H_Geo="0.6757858" Time_DrawDone="133.091" H_Line="0.6321549" />
<item Time_Geo1="133.658" Type_Geo1="Cube" Time_Geo2="139.9974" Type_Geo2="Cube" H_Geo="0.7748006" Time_DrawDone="155.2971" H_Line="0.8391276" />
<item Time_Geo1="155.8631" Type_Geo1="Sphere" Time_Geo2="161.0607" Type_Geo2="Sphere" H_Geo="0.4040862" Time_DrawDone="169.7947" H_Line="0.4031232" />
<item Time_Geo1="170.3606" Type_Geo1="Capsule" Time_Geo2="175.1247" Type_Geo2="Capsule" H_Geo="0.9475007" Time_DrawDone="186.5621" H_Line="0.8770677" />
</test>
<test id_menu="1" name_menu="pinch" date="2023_3_14" time="16_44_41">
<plam Handedness="RightPalm" DetectedTime="34.87806" SX="0.07655575" SY="-0.4219401" SZ="0.2071762" LostTime="99.98267" EX="0.251495" EY="-0.2576148" EZ="0.2788724" />
<plam Handedness="RightPalm" DetectedTime="100.0181" SX="0.251495" SY="-0.2576148" SZ="0.2788724" LostTime="100.1163" EX="0.2537126" EY="-0.2553006" EZ="0.2801554" />
<plam Handedness="RightPalm" DetectedTime="100.3484" SX="0.2537126" SY="-0.2553006" SZ="0.2801554" LostTime="100.4828" EX="0.2532921" EY="-0.2586927" EZ="0.2673245" />
<plam Handedness="LeftPalm" DetectedTime="100.3161" SX="0.1735656" SY="-0.3513042" SZ="0.2867997" LostTime="106.142" EX="-0.05256926" EY="-0.3606887" EZ="0.2833454" />
<plam Handedness="LeftPalm" DetectedTime="107.3987" SX="-0.05256926" SY="-0.3606887" SZ="0.2833454" LostTime="113.6632" EX="-0.07782322" EY="-0.3117663" EZ="0.3023687" />
<plam Handedness="RightPalm" DetectedTime="104.1453" SX="0.2532921" SY="-0.2586927" SZ="0.2673245" LostTime="115.8625" EX="0.08004183" EY="-0.3981861" EZ="0.2538866" />
<plam Handedness="LeftPalm" DetectedTime="116.9277" SX="-0.07782322" SY="-0.3117663" SZ="0.3023687" LostTime="117.0614" EX="-0.06426249" EY="-0.2418947" EZ="0.2984847" />
<plam Handedness="RightPalm" DetectedTime="119.0671" SX="0.08004183" SY="-0.3981861" SZ="0.2538866" LostTime="122.7942" EX="0.209739" EY="-0.03687073" EZ="0.2199602" />
<plam Handedness="LeftPalm" DetectedTime="117.094" SX="-0.06426249" SY="-0.2418947" SZ="0.2984847" LostTime="123.3622" EX="-0.05604112" EY="-0.435471" EZ="0.2483029" />
<plam Handedness="RightPalm" DetectedTime="123.1612" SX="0.209739" SY="-0.03687073" SZ="0.2199602" LostTime="124.8579" EX="0.1836865" EY="-0.4364395" EZ="0.2055839" />
<plam Handedness="LeftPalm" DetectedTime="125.0588" SX="-0.05604112" SY="-0.435471" SZ="0.2483029" LostTime="125.1248" EX="-0.05604112" EY="-0.435471" EZ="0.2483029" />
<plam Handedness="LeftPalm" DetectedTime="125.1596" SX="-0.05604112" SY="-0.435471" SZ="0.2483029" LostTime="125.2247" EX="-0.05485446" EY="-0.372257" EZ="0.2501673" />
<plam Handedness="LeftPalm" DetectedTime="125.358" SX="-0.05485446" SY="-0.372257" SZ="0.2501673" LostTime="125.3908" EX="-0.05485446" EY="-0.372257" EZ="0.2501673" />
<plam Handedness="LeftPalm" DetectedTime="125.5574" SX="-0.05485446" SY="-0.372257" SZ="0.2501673" LostTime="131.158" EX="-0.1420111" EY="-0.3026521" EZ="0.2806275" />
<plam Handedness="RightPalm" DetectedTime="126.5269" SX="0.1836865" SY="-0.4364395" SZ="0.2055839" LostTime="133.658" EX="0.06712513" EY="-0.3853061" EZ="0.2786374" />
<plam Handedness="RightPalm" DetectedTime="136.6646" SX="0.06712513" SY="-0.3853061" SZ="0.2786374" LostTime="137.7371" EX="0.08858059" EY="-0.0512672" EZ="0.2079546" />
<plam Handedness="LeftPalm" DetectedTime="134.3971" SX="-0.1420111" SY="-0.3026521" SZ="0.2806275" LostTime="138.7327" EX="-0.07749137" EY="-0.3491052" EZ="0.3026391" />
<plam Handedness="RightPalm" DetectedTime="138.5311" SX="0.08858059" SY="-0.0512672" SZ="0.2079546" LostTime="141.4306" EX="0.286624" EY="-0.1899896" EZ="0.2838109" />
<plam Handedness="LeftPalm" DetectedTime="140.9643" SX="-0.07749137" SY="-0.3491052" SZ="0.3026391" LostTime="153.5302" EX="-0.05062942" EY="-0.2772741" EZ="0.3320418" />
<plam Handedness="RightPalm" DetectedTime="141.7642" SX="0.286624" SY="-0.1899896" SZ="0.2838109" LostTime="156.1294" EX="0.1115678" EY="-0.4126436" EZ="0.2556867" />
<plam Handedness="RightPalm" DetectedTime="156.496" SX="0.1115678" SY="-0.4126436" SZ="0.2556867" LostTime="156.7625" EX="0.1115678" EY="-0.4126436" EZ="0.2556867" />
<plam Handedness="RightPalm" DetectedTime="158.2343" SX="0.1115678" SY="-0.4126436" SZ="0.2556867" LostTime="159.8298" EX="0.126141" EY="-0.1219666" EZ="0.224764" />
<plam Handedness="LeftPalm" DetectedTime="156.6626" SX="-0.05062942" SY="-0.2772741" SZ="0.3320418" LostTime="160.5632" EX="-0.06502125" EY="-0.3690178" EZ="0.290602" />
<plam Handedness="RightPalm" DetectedTime="160.402" SX="0.126141" SY="-0.1219666" SZ="0.224764" LostTime="162.2631" EX="0.153663" EY="-0.4813332" EZ="0.2180292" />
<plam Handedness="LeftPalm" DetectedTime="162.3976" SX="-0.06502125" SY="-0.3690178" SZ="0.290602" LostTime="167.8306" EX="-0.07961424" EY="-0.3707618" EZ="0.2681364" />
</test>
<test id_menu="2" name_menu="gaze+pinch" date="2023_3_14" time="16_46_21">
<item Time_Geo1="198.5961" Type_Geo1="Cube" Time_Geo2="206.6574" Type_Geo2="Cube" H_Geo="0.5008803" Time_DrawDone="217.7782" H_Line="0.509787" />
<item Time_Geo1="218.3112" Type_Geo1="Capsule" Time_Geo2="224.2461" Type_Geo2="Capsule" H_Geo="0.6757858" Time_DrawDone="231.9783" H_Line="0.6024916" />
<item Time_Geo1="232.5463" Type_Geo1="Cube" Time_Geo2="249.9495" Type_Geo2="Cube" H_Geo="0.7748006" Time_DrawDone="260.7002" H_Line="0.8372936" />
<item Time_Geo1="261.2467" Type_Geo1="Sphere" Time_Geo2="270.3894" Type_Geo2="Sphere" H_Geo="0.4040862" Time_DrawDone="277.7195" H_Line="0.3815593" />
<item Time_Geo1="278.2574" Type_Geo1="Capsule" Time_Geo2="284.2891" Type_Geo2="Capsule" H_Geo="0.9475007" Time_DrawDone="294.3248" H_Line="0.8680592" />
</test>
<test id_menu="2" name_menu="gaze+pinch" date="2023_3_14" time="16_46_21">
<plam Handedness="RightPalm" DetectedTime="163.7954" SX="0.153663" SY="-0.4813332" SZ="0.2180292" LostTime="199.6625" EX="0.3386618" EY="-0.2940358" EZ="0.2380949" />
<plam Handedness="RightPalm" DetectedTime="200.5286" SX="0.3386618" SY="-0.2940358" SZ="0.2380949" LostTime="200.7638" EX="0.2457447" EY="-0.4954429" EZ="0.1667908" />
<plam Handedness="RightPalm" DetectedTime="200.8299" SX="0.2457447" SY="-0.4954429" SZ="0.1667908" LostTime="200.996" EX="0.2428392" EY="-0.4960332" EZ="0.1834559" />
<plam Handedness="LeftPalm" DetectedTime="200.996" SX="-0.06203588" SY="-0.3892163" SZ="0.2831571" LostTime="206.1578" EX="-0.06168339" EY="-0.3884457" EZ="0.2398584" />
<plam Handedness="RightPalm" DetectedTime="204.7624" SX="0.2428392" SY="-0.4960332" SZ="0.1834559" LostTime="207.6948" EX="0.1643705" EY="-0.6031695" EZ="0.1729709" />
<plam Handedness="RightPalm" DetectedTime="209.52" SX="0.1643705" SY="-0.6031695" SZ="0.1729709" LostTime="211.4122" EX="0.183596" EY="-0.1187202" EZ="0.2443407" />
<plam Handedness="LeftPalm" DetectedTime="207.6948" SX="-0.06168339" SY="-0.3884457" SZ="0.2398584" LostTime="216.0445" EX="-0.1003481" EY="-0.4211703" EZ="0.2411403" />
<plam Handedness="RightPalm" DetectedTime="212.3141" SX="0.183596" SY="-0.1187202" SZ="0.2443407" LostTime="218.4445" EX="0.0879577" EY="-0.3853804" EZ="0.2822185" />
<plam Handedness="LeftPalm" DetectedTime="219.2446" SX="-0.1003481" SY="-0.4211703" SZ="0.2411403" LostTime="223.7761" EX="-0.05000813" EY="-0.321342" EZ="0.287106" />
<plam Handedness="RightPalm" DetectedTime="222.2773" SX="0.0879577" SY="-0.3853804" SZ="0.2822185" LostTime="225.3767" EX="0.1855716" EY="-0.4678023" EZ="0.1901254" />
<plam Handedness="RightPalm" DetectedTime="226.8128" SX="0.1855716" SY="-0.4678023" SZ="0.1901254" LostTime="229.7463" EX="0.09913649" EY="-0.09033193" EZ="0.28473" />
<plam Handedness="LeftPalm" DetectedTime="225.3767" SX="-0.05000813" SY="-0.321342" SZ="0.287106" LostTime="230.3792" EX="-0.1068883" EY="-0.3140479" EZ="0.3041503" />
<plam Handedness="RightPalm" DetectedTime="230.0804" SX="0.09913649" SY="-0.09033193" SZ="0.28473" LostTime="232.7473" EX="0.06826448" EY="-0.5575857" EZ="0.2641316" />
<plam Handedness="LeftPalm" DetectedTime="233.1802" SX="-0.1068883" SY="-0.3140479" SZ="0.3041503" LostTime="233.2459" EX="-0.100429" EY="-0.1662697" EZ="0.271444" />
<plam Handedness="RightPalm" DetectedTime="235.5979" SX="0.06826448" SY="-0.5575857" SZ="0.2641316" LostTime="237.2236" EX="0.07766655" EY="-0.1653745" EZ="0.2168766" />
<plam Handedness="LeftPalm" DetectedTime="233.4451" SX="-0.100429" SY="-0.1662697" SZ="0.271444" LostTime="238.1166" EX="0.007414225" EY="-0.478846" EZ="0.1992966" />
<plam Handedness="RightPalm" DetectedTime="240.1487" SX="0.07766655" SY="-0.1653745" SZ="0.2168766" LostTime="241.7123" EX="0.1621452" EY="-0.4673416" EZ="0.1899586" />
<plam Handedness="LeftPalm" DetectedTime="238.9518" SX="0.007414225" SY="-0.478846" SZ="0.1992966" LostTime="242.5476" EX="-0.07474322" EY="-0.3364165" EZ="0.3080715" />
<plam Handedness="RightPalm" DetectedTime="245.679" SX="0.1621452" SY="-0.4673416" SZ="0.1899586" LostTime="248.4167" EX="0.105657" EY="-0.07175861" EZ="0.2629155" />
<plam Handedness="RightPalm" DetectedTime="248.5184" SX="0.105657" SY="-0.07175861" SZ="0.2629155" LostTime="248.6836" EX="0.1124986" EY="-0.07037206" EZ="0.2526661" />
<plam Handedness="LeftPalm" DetectedTime="242.5789" SX="-0.07474322" SY="-0.3364165" SZ="0.3080715" LostTime="249.4825" EX="-0.07943277" EY="-0.3214224" EZ="0.2878037" />
<plam Handedness="RightPalm" DetectedTime="248.7175" SX="0.1124986" SY="-0.07037206" SZ="0.2526661" LostTime="250.8815" EX="0.1227659" EY="-0.599288" EZ="0.1792651" />
<plam Handedness="RightPalm" DetectedTime="251.1514" SX="0.1227659" SY="-0.599288" SZ="0.1792651" LostTime="251.1847" EX="0.1227659" EY="-0.599288" EZ="0.1792651" />
<plam Handedness="LeftPalm" DetectedTime="251.1847" SX="-0.07943277" SY="-0.3214224" SZ="0.2878037" LostTime="258.6647" EX="-0.1113915" EY="-0.4377198" EZ="0.2374346" />
<plam Handedness="RightPalm" DetectedTime="255.0306" SX="0.1227659" SY="-0.599288" SZ="0.1792651" LostTime="261.3148" EX="0.1307171" EY="-0.3868572" EZ="0.2968586" />
<plam Handedness="RightPalm" DetectedTime="265.1467" SX="0.1307171" SY="-0.3868572" SZ="0.2968586" LostTime="268.1198" EX="0.08701013" EY="-0.03939727" EZ="0.3543727" />
<plam Handedness="LeftPalm" DetectedTime="262.947" SX="-0.1113915" SY="-0.4377198" SZ="0.2374346" LostTime="269.9578" EX="-0.112778" EY="-0.2416162" EZ="0.3473982" />
<plam Handedness="RightPalm" DetectedTime="268.5869" SX="0.08701013" SY="-0.03939727" SZ="0.3543727" LostTime="271.4237" EX="0.306284" EY="-0.2275391" EZ="0.2803196" />
<plam Handedness="RightPalm" DetectedTime="273.0554" SX="0.306284" SY="-0.2275391" SZ="0.2803196" LostTime="275.2577" EX="0.2184373" EY="-0.1001188" EZ="0.3484235" />
<plam Handedness="LeftPalm" DetectedTime="271.3887" SX="-0.112778" SY="-0.2416162" SZ="0.3473982" LostTime="276.2533" EX="-0.08800004" EY="-0.3399307" EZ="0.2969807" />
</test>
<test id_menu="3" name_menu="speech+pinch" date="2023_3_14" time="16_48_3">
<item Time_Geo1="300.7907" Type_Geo1="Cube" Time_Geo2="317.0861" Type_Geo2="Cube" H_Geo="0.5008803" Time_DrawDone="335.2169" H_Line="0.4971054" />
<item Time_Geo1="335.7587" Type_Geo1="Capsule" Time_Geo2="346.1549" Type_Geo2="Capsule" H_Geo="0.6757858" Time_DrawDone="358.0239" H_Line="0.6093274" />
<item Time_Geo1="358.5573" Type_Geo1="Cube" Time_Geo2="365.5538" Type_Geo2="Cube" H_Geo="0.7748006" Time_DrawDone="381.4522" H_Line="0.8385747" />
<item Time_Geo1="381.9847" Type_Geo1="Sphere" Time_Geo2="388.4523" Type_Geo2="Sphere" H_Geo="0.4040862" Time_DrawDone="415.0188" H_Line="0.4003016" />
<item Time_Geo1="415.5526" Type_Geo1="Capsule" Time_Geo2="432.1519" Type_Geo2="Capsule" H_Geo="0.9475007" Time_DrawDone="466.6841" H_Line="0.8405979" />
</test>
<test id_menu="3" name_menu="speech+pinch" date="2023_3_14" time="16_48_3">
<plam Handedness="RightPalm" DetectedTime="275.7232" SX="0.2184373" SY="-0.1001188" SZ="0.3484235" LostTime="301.7234" EX="0.1710723" EY="-0.465977" EZ="0.2614718" />
<plam Handedness="LeftPalm" DetectedTime="303.9232" SX="0.01276887" SY="-0.07098423" SZ="0.1614355" LostTime="316.587" EX="-0.07194316" EY="-0.4088579" EZ="0.2338254" />
<plam Handedness="RightPalm" DetectedTime="316.587" SX="0.1710723" SY="-0.465977" SZ="0.2614718" LostTime="317.9193" EX="0.1359394" EY="-0.5216215" EZ="0.2269986" />
<plam Handedness="RightPalm" DetectedTime="321.7215" SX="0.1359394" SY="-0.5216215" SZ="0.2269986" LostTime="323.4527" EX="0.2389728" EY="-0.12471" EZ="0.2800354" />
<plam Handedness="LeftPalm" DetectedTime="318.1213" SX="-0.07194316" SY="-0.4088579" SZ="0.2338254" LostTime="327.7887" EX="-0.03693908" EY="-0.4122236" EZ="0.2507878" />
<plam Handedness="RightPalm" DetectedTime="327.7887" SX="0.2389728" SY="-0.12471" SZ="0.2800354" LostTime="328.0554" EX="0.1618235" EY="-0.4732238" EZ="0.2032567" />
<plam Handedness="LeftPalm" DetectedTime="328.0886" SX="-0.03693908" SY="-0.4122236" SZ="0.2507878" LostTime="328.1568" EX="-0.03693908" EY="-0.4122236" EZ="0.2507878" />
<plam Handedness="RightPalm" DetectedTime="330.9193" SX="0.1618235" SY="-0.4732238" SZ="0.2032567" LostTime="331.4555" EX="0.2482997" EY="-0.2108441" EZ="0.1848204" />
<plam Handedness="RightPalm" DetectedTime="331.7201" SX="0.2482997" SY="-0.2108441" SZ="0.1848204" LostTime="333.4858" EX="0.09504647" EY="-0.1631676" EZ="0.1662293" />
<plam Handedness="LeftPalm" DetectedTime="328.1887" SX="-0.03693908" SY="-0.4122236" SZ="0.2507878" LostTime="333.8175" EX="-0.0401892" EY="-0.3770092" EZ="0.2895065" />
<plam Handedness="RightPalm" DetectedTime="333.6857" SX="0.09504647" SY="-0.1631676" SZ="0.1662293" LostTime="335.7151" EX="0.01529652" EY="-0.4138146" EZ="0.2555683" />
<plam Handedness="LeftPalm" DetectedTime="337.7168" SX="-0.0401892" SY="-0.3770092" SZ="0.2895065" LostTime="345.5881" EX="-0.07145354" EY="-0.3157215" EZ="0.3044487" />
<plam Handedness="RightPalm" DetectedTime="345.488" SX="0.01529652" SY="-0.4138146" SZ="0.2555683" LostTime="347.7241" EX="0.1029989" EY="-0.5430751" EZ="0.2242493" />
<plam Handedness="RightPalm" DetectedTime="347.9236" SX="0.1029989" SY="-0.5430751" SZ="0.2242493" LostTime="348.0922" EX="0.1029989" EY="-0.5430751" EZ="0.2242493" />
<plam Handedness="LeftPalm" DetectedTime="348.0922" SX="-0.07145354" SY="-0.3157215" SZ="0.3044487" LostTime="353.7588" EX="-0.09313947" EY="-0.2493981" EZ="0.3492281" />
<plam Handedness="RightPalm" DetectedTime="351.2574" SX="0.1029989" SY="-0.5430751" SZ="0.2242493" LostTime="358.6915" EX="0.07988869" EY="-0.450515" EZ="0.2622379" />
<plam Handedness="LeftPalm" DetectedTime="359.0211" SX="-0.09313947" SY="-0.2493981" SZ="0.3492281" LostTime="365.0541" EX="-0.08653944" EY="-0.4288945" EZ="0.2441448" />
<plam Handedness="RightPalm" DetectedTime="365.0541" SX="0.07988869" SY="-0.450515" SZ="0.2622379" LostTime="366.4826" EX="0.1606682" EY="-0.5271467" EZ="0.1925918" />
<plam Handedness="RightPalm" DetectedTime="369.55" SX="0.1606682" SY="-0.5271467" SZ="0.1925918" LostTime="369.8516" EX="0.0802855" EY="-0.07466046" EZ="0.2593605" />
<plam Handedness="LeftPalm" DetectedTime="366.717" SX="-0.08653944" SY="-0.4288945" SZ="0.2441448" LostTime="375.1841" EX="-0.1063545" EY="-0.4255775" EZ="0.2353766" />
<plam Handedness="RightPalm" DetectedTime="371.8485" SX="0.0802855" SY="-0.07466046" SZ="0.2593605" LostTime="379.6505" EX="0.1001827" EY="-0.3234482" EZ="0.2988378" />
<plam Handedness="RightPalm" DetectedTime="379.7514" SX="0.1001827" SY="-0.3234482" SZ="0.2988378" LostTime="381.8841" EX="0.1820823" EY="-0.04857275" EZ="0.283025" />
<plam Handedness="RightPalm" DetectedTime="382.2174" SX="0.1820823" SY="-0.04857275" SZ="0.283025" LostTime="382.4856" EX="0.159729" EY="-0.5201573" EZ="0.2768952" />
<plam Handedness="RightPalm" DetectedTime="382.8531" SX="0.159729" SY="-0.5201573" SZ="0.2768952" LostTime="382.9194" EX="0.159729" EY="-0.5201573" EZ="0.2768952" />
<plam Handedness="RightPalm" DetectedTime="382.9543" SX="0.159729" SY="-0.5201573" SZ="0.2768952" LostTime="383.0224" EX="0.159729" EY="-0.5201573" EZ="0.2768952" />
<plam Handedness="LeftPalm" DetectedTime="383.0224" SX="-0.1063545" SY="-0.4255775" SZ="0.2353766" LostTime="387.8856" EX="-0.0823067" EY="-0.3664609" EZ="0.2672928" />
<plam Handedness="RightPalm" DetectedTime="387.8856" SX="0.159729" SY="-0.5201573" SZ="0.2768952" LostTime="389.4528" EX="0.3147371" EY="-0.3238671" EZ="0.2543449" />
<plam Handedness="RightPalm" DetectedTime="392.8891" SX="0.3147371" SY="-0.3238671" SZ="0.2543449" LostTime="394.1236" EX="0.2025102" EY="-0.08452841" EZ="0.3358383" />
<plam Handedness="LeftPalm" DetectedTime="389.4528" SX="-0.0823067" SY="-0.3664609" SZ="0.2672928" LostTime="395.8224" EX="-0.07209842" EY="-0.4101767" EZ="0.2857964" />
<plam Handedness="RightPalm" DetectedTime="395.8224" SX="0.2025102" SY="-0.08452841" SZ="0.3358383" LostTime="396.0899" EX="0.1590665" EY="-0.4421564" EZ="0.2269682" />
<plam Handedness="RightPalm" DetectedTime="398.8893" SX="0.1590665" SY="-0.4421564" SZ="0.2269682" LostTime="400.189" EX="0.1455698" EY="-0.07844377" EZ="0.2097017" />
<plam Handedness="RightPalm" DetectedTime="400.2568" SX="0.1455698" SY="-0.07844377" SZ="0.2097017" LostTime="401.4241" EX="0.2065924" EY="-0.08117215" EZ="0.2027453" />
<plam Handedness="LeftPalm" DetectedTime="396.0899" SX="-0.07209842" SY="-0.4101767" SZ="0.2857964" LostTime="403.3556" EX="-0.09959359" EY="-0.3580822" EZ="0.2973758" />
<plam Handedness="RightPalm" DetectedTime="403.0229" SX="0.2065924" SY="-0.08117215" SZ="0.2027453" LostTime="403.4223" EX="0.1581121" EY="-0.4943396" EZ="0.2154905" />
<plam Handedness="RightPalm" DetectedTime="405.9888" SX="0.1581121" SY="-0.4943396" SZ="0.2154905" LostTime="406.3561" EX="0.1672713" EY="-0.05480603" EZ="0.2021254" />
<plam Handedness="LeftPalm" DetectedTime="403.9238" SX="-0.09959359" SY="-0.3580822" SZ="0.2973758" LostTime="410.7877" EX="-0.09530007" EY="-0.4294628" EZ="0.2389277" />
<plam Handedness="RightPalm" DetectedTime="406.4893" SX="0.1672713" SY="-0.05480603" SZ="0.2021254" LostTime="413.1533" EX="0.1238262" EY="-0.3754546" EZ="0.2684276" />
</test>
<test id_menu="4" name_menu="bangles" date="2023_3_14" time="16_51_3">
<item Time_Geo1="480.5804" Type_Geo1="Cube" Time_Geo2="504.0177" Type_Geo2="Cube" H_Geo="0.5008803" Time_DrawDone="543.484" H_Line="0.4298529" />
<item Time_Geo1="544.0187" Type_Geo1="Capsule" Time_Geo2="551.2493" Type_Geo2="Capsule" H_Geo="0.6757858" Time_DrawDone="566.5778" H_Line="0.6600204" />
<item Time_Geo1="567.1112" Type_Geo1="Cube" Time_Geo2="574.2111" Type_Geo2="Cube" H_Geo="0.7748006" Time_DrawDone="589.6138" H_Line="0.8155193" />
<item Time_Geo1="590.1471" Type_Geo1="Sphere" Time_Geo2="597.8851" Type_Geo2="Sphere" H_Geo="0.4040862" Time_DrawDone="613.4413" H_Line="0.2251672" />
<item Time_Geo1="613.9785" Type_Geo1="Capsule" Time_Geo2="620.8147" Type_Geo2="Capsule" H_Geo="0.9475007" Time_DrawDone="666.2729" H_Line="0.889469" />
</test>
<test id_menu="4" name_menu="bangles" date="2023_3_14" time="16_51_3">
<plam Handedness="RightPalm" DetectedTime="413.1894" SX="0.1238262" SY="-0.3754546" SZ="0.2684276" LostTime="481.9145" EX="0.2250034" EY="-0.4668577" EZ="0.2653239" />
<plam Handedness="RightPalm" DetectedTime="498.3149" SX="0.2250034" SY="-0.4668577" SZ="0.2653239" LostTime="498.4478" EX="0.07488804" EY="-0.2255443" EZ="0.2090246" />
<plam Handedness="RightPalm" DetectedTime="498.5811" SX="0.07488804" SY="-0.2255443" SZ="0.2090246" LostTime="498.8807" EX="0.0465105" EY="-0.2177656" EZ="0.2471933" />
<plam Handedness="RightPalm" DetectedTime="499.5504" SX="0.0465105" SY="-0.2177656" SZ="0.2471933" LostTime="500.3503" EX="0.1289067" EY="-0.4240218" EZ="0.2455488" />
<plam Handedness="LeftPalm" DetectedTime="482.8132" SX="-0.008853512" SY="-0.4446858" SZ="0.2439526" LostTime="503.2166" EX="-0.03994724" EY="-0.500555" EZ="0.2335285" />
<plam Handedness="RightPalm" DetectedTime="503.3165" SX="0.1289067" SY="-0.4240218" SZ="0.2455488" LostTime="504.9536" EX="0.08218495" EY="-0.5242473" EZ="0.2372534" />
<plam Handedness="LeftPalm" DetectedTime="505.2531" SX="-0.03994724" SY="-0.500555" SZ="0.2335285" LostTime="523.9579" EX="-0.05824919" EY="-0.129221" EZ="0.3983088" />
<plam Handedness="RightPalm" DetectedTime="527.7921" SX="0.08218495" SY="-0.5242473" SZ="0.2372534" LostTime="527.9267" EX="0.1643977" EY="-0.4536451" EZ="0.2793882" />
<plam Handedness="RightPalm" DetectedTime="531.2577" SX="0.1643977" SY="-0.4536451" SZ="0.2793882" LostTime="531.358" EX="0.1643977" EY="-0.4536451" EZ="0.2793882" />
<plam Handedness="LeftPalm" DetectedTime="524.1913" SX="-0.05824919" SY="-0.129221" SZ="0.3983088" LostTime="541.6158" EX="-0.04386255" EY="-0.4173181" EZ="0.2640454" />
<plam Handedness="RightPalm" DetectedTime="541.9161" SX="0.1643977" SY="-0.4536451" SZ="0.2793882" LostTime="543.9171" EX="-0.02633439" EY="-0.1967918" EZ="0.2592406" />
<plam Handedness="LeftPalm" DetectedTime="545.4501" SX="-0.04386255" SY="-0.4173181" SZ="0.2640454" LostTime="550.5494" EX="-0.0995372" EY="-0.4166328" EZ="0.2732223" />
<plam Handedness="RightPalm" DetectedTime="550.5845" SX="-0.02633439" SY="-0.1967918" SZ="0.2592406" LostTime="552.4182" EX="0.1135058" EY="-0.4675419" EZ="0.2867061" />
<plam Handedness="LeftPalm" DetectedTime="552.5512" SX="-0.0995372" SY="-0.4166328" SZ="0.2732223" LostTime="562.7787" EX="-0.08480065" EY="-0.4291495" EZ="0.2612426" />
<plam Handedness="RightPalm" DetectedTime="562.9779" SX="0.1135058" SY="-0.4675419" SZ="0.2867061" LostTime="567.3145" EX="-0.04602948" EY="-0.3748654" EZ="0.2601059" />
<plam Handedness="LeftPalm" DetectedTime="568.2775" SX="-0.08480065" SY="-0.4291495" SZ="0.2612426" LostTime="572.9447" EX="-0.1017904" EY="-0.3961751" EZ="0.2568" />
<plam Handedness="RightPalm" DetectedTime="573.0109" SX="-0.04602948" SY="-0.3748654" SZ="0.2601059" LostTime="575.1445" EX="0.1098714" EY="-0.3249022" EZ="0.2748399" />
<plam Handedness="LeftPalm" DetectedTime="575.9783" SX="-0.1017904" SY="-0.3961751" SZ="0.2568" LostTime="587.3848" EX="-0.07046892" EY="-0.526482" EZ="0.226741" />
<plam Handedness="RightPalm" DetectedTime="587.5858" SX="0.1098714" SY="-0.3249022" SZ="0.2748399" LostTime="590.3143" EX="-0.04405334" EY="-0.3430937" EZ="0.2010269" />
<plam Handedness="LeftPalm" DetectedTime="591.1804" SX="-0.07046892" SY="-0.526482" SZ="0.226741" LostTime="597.3489" EX="-0.05653439" EY="-0.4659257" EZ="0.2381081" />
<plam Handedness="RightPalm" DetectedTime="597.384" SX="-0.04405334" SY="-0.3430937" SZ="0.2010269" LostTime="598.9188" EX="0.1373117" EY="-0.5095411" EZ="0.2511284" />
<plam Handedness="LeftPalm" DetectedTime="599.0189" SX="-0.05653439" SY="-0.4659257" SZ="0.2381081" LostTime="610.7413" EX="-0.05195542" EY="-0.502552" EZ="0.2389538" />
<plam Handedness="RightPalm" DetectedTime="610.9745" SX="0.1373117" SY="-0.5095411" SZ="0.2511284" LostTime="613.9785" EX="-0.004703316" EY="-0.2928194" EZ="0.2882046" />
</test>
<test id_menu="5" name_menu="compass+ring" date="2023_3_14" time="16_54_25">
<item Time_Geo1="682.6754" Type_Geo1="Cube" Time_Geo2="690.4774" Type_Geo2="Cube" H_Geo="0.5008803" Time_DrawDone="703.0756" H_Line="0.5358995" />
<item Time_Geo1="703.6092" Type_Geo1="Capsule" Time_Geo2="727.0759" Type_Geo2="Capsule" H_Geo="0.6757858" Time_DrawDone="742.8701" H_Line="0.6532664" />
<item Time_Geo1="743.4044" Type_Geo1="Cube" Time_Geo2="759.7881" Type_Geo2="Cube" H_Geo="0.7748006" Time_DrawDone="773.3869" H_Line="0.878841" />
<item Time_Geo1="773.9202" Type_Geo1="Sphere" Time_Geo2="782.5525" Type_Geo2="Sphere" H_Geo="0.4040862" Time_DrawDone="803.3642" H_Line="0.4037369" />
<item Time_Geo1="803.8812" Type_Geo1="Capsule" Time_Geo2="811.8802" Type_Geo2="Capsule" H_Geo="0.9475007" Time_DrawDone="831.7311" H_Line="0.8099568" />
</test>
<test id_menu="5" name_menu="compass+ring" date="2023_3_14" time="16_54_25">
<plam LostTime="683.809" EX="0.2153733" EY="-0.3830465" EZ="0.2227947" />
<plam Handedness="LeftPalm" DetectedTime="684.2747" SX="-0.05951744" SY="-0.4150898" SZ="0.2824493" LostTime="690.0442" EX="-0.08544853" EY="-0.3060257" EZ="0.3050128" />
<plam Handedness="RightPalm" DetectedTime="690.0442" SX="0.2153733" SY="-0.3830465" SZ="0.2227947" LostTime="691.4113" EX="0.1404262" EY="-0.54221" EZ="0.234615" />
<plam Handedness="LeftPalm" DetectedTime="691.6439" SX="-0.08544853" SY="-0.3060257" SZ="0.3050128" LostTime="699.6087" EX="-0.08056187" EY="-0.2547335" EZ="0.3028742" />
<plam Handedness="RightPalm" DetectedTime="699.8423" SX="0.1404262" SY="-0.54221" SZ="0.234615" LostTime="703.4763" EX="0.1872171" EY="-0.197969" EZ="0.1985819" />
<plam Handedness="RightPalm" DetectedTime="703.6092" SX="0.1872171" SY="-0.197969" SZ="0.1985819" LostTime="703.7115" EX="0.1864271" EY="-0.2083302" EZ="0.2285699" />
<plam Handedness="LeftPalm" DetectedTime="704.9434" SX="-0.08056187" SY="-0.2547335" SZ="0.3028742" LostTime="709.7452" EX="0.05035546" EY="-0.5278698" EZ="0.2443387" />
<plam Handedness="RightPalm" DetectedTime="713.1775" SX="0.1864271" SY="-0.2083302" SZ="0.2285699" LostTime="713.8109" EX="0.1270777" EY="-0.3517072" EZ="0.2751287" />
<plam Handedness="LeftPalm" DetectedTime="712.4774" SX="0.05035546" SY="-0.5278698" SZ="0.2443387" LostTime="720.1107" EX="-0.04261585" EY="-0.387334" EZ="0.2273674" />
<plam Handedness="LeftPalm" DetectedTime="720.1774" SX="-0.04261585" SY="-0.387334" SZ="0.2273674" LostTime="726.4421" EX="-0.05651246" EY="-0.4987181" EZ="0.2166077" />
<plam Handedness="RightPalm" DetectedTime="726.4421" SX="0.1270777" SY="-0.3517072" SZ="0.2751287" LostTime="728.0345" EX="0.08559" EY="-0.434339" EZ="0.2137844" />
<plam Handedness="LeftPalm" DetectedTime="728.4678" SX="-0.05651246" SY="-0.4987181" SZ="0.2166077" LostTime="734.97" EX="-0.02995808" EY="-0.4964167" EZ="0.2009178" />
<plam Handedness="RightPalm" DetectedTime="735.1376" SX="0.08559" SY="-0.434339" SZ="0.2137844" LostTime="743.2698" EX="0.07980924" EY="-0.2258768" EZ="0.2454493" />
<plam Handedness="RightPalm" DetectedTime="743.3412" SX="0.07980924" SY="-0.2258768" SZ="0.2454493" LostTime="744.3012" EX="0.07562254" EY="-0.4782209" EZ="0.2302275" />
<plam Handedness="LeftPalm" DetectedTime="744.6671" SX="-0.02995808" SY="-0.4964167" SZ="0.2009178" LostTime="748.9191" EX="-0.1352524" EY="-0.1484973" EZ="0.3241736" />
<plam Handedness="LeftPalm" DetectedTime="750.9047" SX="-0.1352524" SY="-0.1484973" SZ="0.3241736" LostTime="752.1384" EX="-0.06120089" EY="-0.49983" EZ="0.2026015" />
<plam Handedness="LeftPalm" DetectedTime="754.2045" SX="-0.06120089" SY="-0.49983" SZ="0.2026015" LostTime="759.0373" EX="-0.06906418" EY="-0.4940878" EZ="0.2380689" />
<plam Handedness="RightPalm" DetectedTime="759.2042" SX="0.07562254" SY="-0.4782209" SZ="0.2302275" LostTime="760.6349" EX="0.1668244" EY="-0.133809" EZ="0.2122639" />
<plam Handedness="LeftPalm" DetectedTime="761.5349" SX="-0.06906418" SY="-0.4940878" SZ="0.2380689" LostTime="770.9686" EX="-0.02661195" EY="-0.5510437" EZ="0.2420505" />
<plam Handedness="RightPalm" DetectedTime="770.8364" SX="0.1668244" SY="-0.133809" SZ="0.2122639" LostTime="774.1366" EX="0.1718812" EY="-0.2836642" EZ="0.239033" />
<plam Handedness="RightPalm" DetectedTime="774.1871" SX="0.1718812" SY="-0.2836642" SZ="0.239033" LostTime="774.2036" EX="0.1718812" EY="-0.2836642" EZ="0.239033" />
<plam Handedness="RightPalm" DetectedTime="774.3369" SX="0.1718812" SY="-0.2836642" SZ="0.239033" LostTime="774.7371" EX="0.1035001" EY="0.0591075" EZ="0.1439502" />
<plam Handedness="RightPalm" DetectedTime="776.4697" SX="0.1035001" SY="0.0591075" SZ="0.1439502" LostTime="776.8199" EX="0.1035259" EY="-0.5267221" EZ="0.2335708" />
<plam Handedness="LeftPalm" DetectedTime="777.0035" SX="-0.02661195" SY="-0.5510437" SZ="0.2420505" LostTime="782.0356" EX="-0.06105529" EY="-0.4799297" EZ="0.2322753" />
<plam Handedness="RightPalm" DetectedTime="782.0356" SX="0.1035259" SY="-0.5267221" SZ="0.2335708" LostTime="784.3354" EX="0.1189786" EY="-0.5204296" EZ="0.2651331" />
<plam Handedness="LeftPalm" DetectedTime="784.3188" SX="-0.06105529" SY="-0.4799297" SZ="0.2322753" LostTime="793.3661" EX="-0.1460431" EY="-0.1457093" EZ="0.3339515" />
<plam Handedness="LeftPalm" DetectedTime="793.4989" SX="-0.1460431" SY="-0.1457093" SZ="0.3339515" LostTime="800.1309" EX="-0.09888813" EY="-0.4535989" EZ="0.2230973" />
</test>
<test id_menu="0" name_menu="auto" date="2023_3_14" time="16_58_15">
<item Time_Geo1="912.6066" Type_Geo1="Cube" Time_Geo2="914.2405" Type_Geo2="Cube" H_Geo="0.5008803" Time_DrawDone="916.3071" H_Line="0.5008803" />
<item Time_Geo1="916.8247" Type_Geo1="Capsule" Time_Geo2="917.7783" Type_Geo2="Capsule" H_Geo="0.6757858" Time_DrawDone="919.7315" H_Line="0.6757858" />
<item Time_Geo1="920.2644" Type_Geo1="Cube" Time_Geo2="921.0814" Type_Geo2="Cube" H_Geo="0.7748006" Time_DrawDone="923.1097" H_Line="0.7748006" />
<item Time_Geo1="923.6279" Type_Geo1="Sphere" Time_Geo2="924.3313" Type_Geo2="Sphere" H_Geo="0.4040862" Time_DrawDone="928.5617" H_Line="0.4040862" />
<item Time_Geo1="929.0954" Type_Geo1="Capsule" Time_Geo2="929.8952" Type_Geo2="Capsule" H_Geo="0.9475007" Time_DrawDone="932.0267" H_Line="0.9475007" />
</test>
<test id_menu="0" name_menu="auto" date="2023_3_14" time="16_58_15" />
</user>
</root>
2.1. 读取xml文件
python中自带xml,不需要安装,注意项目中文件名不要为xml.py
import xml.dom.minidom
dom = xml.dom.minidom.parse('test.xml') #解析xml文件
root = dom.documentElement #获取xml文档对象,对象类型是Element对象,并且是根节点
print(root.nodeName) #打印根结点的名字
print(root.nodeValue) #打印根结点的名字,只对文本节点有效(/p,/li等等)
print(root.nodeType) #打印结点类型
2.2 getElementsByTagNameNS:返回某个结点下具有指定标记
的所有节点列表(可以使用这个方法+for来遍历xml文档)
testList = root.getElementsByTagName('test') #获取/root结点下所有标记为test结点,有13个
for item in testList:
testChildList = item.getElementsByTagName('item')#获取每个test下的所有标记为item结点
print(testChildList)
2.3. 获取标签中属性值和文本内容
#xml文件
<students>
<student name='刘备' sex='男' age='35'/>
<student name='吕布' sex='男' age='38'/>
<student name='貂蝉' sex='女' age='22'/>
<p>hello</p>
</students>
import xml.dom.minidom
dom = xml.dom.minidom.parse('test.xml')
root = dom.documentElement #students结点
print(root.getAttribute('name'))
testList = root.getElementsByTagName('student')
print(len(testList))
for item in testList:
print(item.getAttribute('name')) #获取标签对应的属性值
print(item.firstChild.data) #获取标签的text值
ElementTree解析方法
与DOM相比,ET的速度更快,API使用更直接、方便.
Element对象方法:
类方法 | 说明 |
| 遍历该Element所有后代,也可以指定tag进行遍历寻找。 |
| 根据tag或path查找所有的后代。 |
| 遍历所有后代并返回text值。 |
| 查找当前元素下tag或path能够匹配的直系节点 |
| 寻找第一个匹配子元素,返回其text值。匹配对象可以为tag或path。 |
| 查找当前元素下tag或path能够匹配的首个直系节点。 |
| 获取当前元素的text值。 |
| 获取元素指定key对应的属性值,如果没有该属性,则返回default值。 |
| 返回元素属性名称列表 |
| 返回(name,value)列表 |
| 变成可遍历的数组 |
| |
|
实战案例:
from xml.etree import ElementTree as ET
tree = ET.parse('test.xml')
p = tree.findall('./user/test')
create_time = []
#baseTime 为开始和结束中name_menu="auto"的平均值作为baseTime
#求baseTime
baseTime = defaultdict(list)
# root.hasChildNodes()
for oneper in p: #遍历test
id_menu = oneper.attrib['id_menu']
name_menu = oneper.attrib['name_menu']
if name_menu == 'auto' and len(list(oneper))>0:
for child in list(oneper): #遍历item
if 'Time_Geo1' not in child.attrib.keys():
break
time_Geo1 = child.attrib['Time_Geo1']
type_Ge01 = child.attrib['Type_Geo1']
time_Geo2 = child.attrib['Time_Geo2']
h_Geo = child.attrib['H_Geo']
time_DrawDone = child.attrib['Time_DrawDone']
h_Line = child.attrib['H_Line']
if len(baseTime[type_Ge01]) == 0:
baseTime[type_Ge01].append(0)
baseTime[type_Ge01].append(0)
baseTime[type_Ge01].append(0)
# print(float(time_Geo2))
baseTime[type_Ge01][0] += float(time_Geo2)-float(time_Geo1)
baseTime[type_Ge01][1] += float(time_DrawDone) - float(time_Geo2)
baseTime[type_Ge01][2] += 1
for k in baseTime.keys():
baseTime[k][0] = baseTime[k][0]/baseTime[k][2]
baseTime[k][1] = baseTime[k][1]/baseTime[k][2]
# print(baseTime)
#计算time1,time2
# data = pd.DataFrame()
res = []
for oneper in p: # 遍历test
id_menu = oneper.attrib['id_menu']
name_menu = oneper.attrib['name_menu']
if name_menu != 'auto' and len(list(oneper)) > 0:
for child in list(oneper): # 遍历item
if 'Time_Geo1' not in child.attrib.keys():
break
time_Geo1 = child.attrib['Time_Geo1']
type_Ge01 = child.attrib['Type_Geo1']
time_Geo2 = child.attrib['Time_Geo2']
h_Geo = child.attrib['H_Geo']
time_DrawDone = child.attrib['Time_DrawDone']
h_Line = child.attrib['H_Line']
if len(baseTime[name_menu]) == 0:
baseTime[name_menu].append(0)
baseTime[name_menu].append(0)
time01 = float(time_Geo2) - float(time_Geo1) - baseTime[name_menu][0]
time02 = float(time_DrawDone) - float(time_Geo2) - baseTime[name_menu][1]
res.append([id_menu,name_menu,time01,time02])
print('res',res)