[英]Parsing XML to Python object similar to suds
Sorry if this is duplicate but I tried digging the Internet and did not really find something relevant. 很抱歉,如果这是重复的,但是我尝试挖掘Internet,却没有真正找到相关的东西。
I have been using suds and it is fantastic in the sense that it parses the results into objects (besides the element tree). 我一直在使用肥皂水,从它可以将结果解析为对象(除了元素树之外)的意义上来说,这是很棒的。
Now, I have a file that is pretty similar to a SOAP response (sans the envelop, etc.) and I am wondering if there is an easy way to parse it to a suds like object. 现在,我有一个非常类似于SOAP响应的文件(没有信封等),我想知道是否有一种简单的方法可以将其解析为类似suds的对象。 The tags come with their ns.
标签带有它们的ns。 Here is a sample TAG, parsed and prettified with
ElementTree
: 这是一个示例TAG,使用
ElementTree
进行了解析和美化:
<?xml version="1.0" ?>
<ns0:substation objectID="{16A743D9-AAA3-4187-94BF-77F63E25E5A6}" verb="New" xmlns:ns0="http://www.multispeak.org/Version_3.0">
<ns0:extensions>
<ns0:symbolCoord>
<ns0:X>332.50187386</ns0:X>
<ns0:Y>343.8680783</ns0:Y>
</ns0:symbolCoord>
</ns0:extensions>
<ns0:mapLocation>
<ns0:coord>
<ns0:X>111.50187386</ns0:X>
<ns0:Y>11.8680783</ns0:Y>
</ns0:coord>
</ns0:mapLocation>
<ns0:facilityID>07xx</ns0:facilityID>
<ns0:sectionID>07xx</ns0:sectionID>
<ns0:parentSectionID name="ROOT" noun="substation" objectID="{00000001-0000-0000-0000-000000000000}"/>
<ns0:phaseCode>ABC</ns0:phaseCode>
<ns0:zMin>xx</ns0:zMin>
<ns0:zMax>xx</ns0:zMax>
<ns0:units>Ohms</ns0:units>
<ns0:posSeqZ r="8.60217666625977" x="32.0800704956055"/>
<ns0:zeroSeqZ r="11.5711364746094" x="44.1044578552246"/>
<ns0:negSeqZ r="8.60217666625977" x="32.0800704956055"/>
<ns0:busVolts>1.03332996368408</ns0:busVolts>
<ns0:ohGndZ>40</ns0:ohGndZ>
<ns0:ugGndZ>10</ns0:ugGndZ>
<ns0:nomVolts>69.0001009674624</ns0:nomVolts>
<ns0:ldAolloc>false</ns0:ldAolloc>
<ns0:ldCon>D</ns0:ldCon>
<ns0:isRegulated>false</ns0:isRegulated>
<ns0:feederList/>
<ns0:name>07xx</ns0:name>
</ns0:substation>
Aside: Is there a better way to replace the ns0 url for all tags than regex/string replacement on the original xml? 除了:有没有比原始xml上的正则表达式/字符串替换更好的方法来替换所有标签的ns0 url?
Update: Here is the (trimmed) content of file: 更新:这是文件的(修剪)内容:
<MultiSpeakMsgHeader Version="3.0" AppName="XXX" AppVersion="8.1.3.2741.988695b6" Company="XX" xmlns="http://www.multispeak.org/Version_3.0" SessionID="1" PreviousSessionID="1" ObjectsRemaining="0">
<MultiSpeak documentType="dump" connectivityModel="Sectional">
<substation objectID="{16A743D9-AAA3-4187-94BF-77F63E25E5A6}" verb="New">
<extensions>
<symbolCoord>
<X>0.50187386</X><Y>0.8680783</Y>
</symbolCoord>
</extensions>
<mapLocation>
<coord>
<X>0.50187386</X><Y>0.8680783</Y>
</coord>
</mapLocation>
<facilityID>07xx</facilityID>
<sectionID>07xx</sectionID>
<parentSectionID name="ROOT" noun="substation" objectID="{00000001-0000-0000-0000-000000000000}"/>
<phaseCode>ABC</phaseCode>
<zMin>X0X</zMin>
<zMax>X0X</zMax>
<units>Ohms</units>
<posSeqZ r="8.60217666625977" x="32.0800704956055"/>
<zeroSeqZ r="11.5711364746094" x="44.1044578552246"/>
<negSeqZ r="8.60217666625977" x="32.0800704956055"/>
<busVolts>1.03332996368408</busVolts>
<ohGndZ>40</ohGndZ>
<ugGndZ>10</ugGndZ>
<nomVolts>69.0001009674624</nomVolts>
<ldAolloc>false</ldAolloc>
<ldCon>D</ldCon>
<isRegulated>false</isRegulated>
<feederList/>
<name>07xx (X0X)</name>
</substation>
<transformerBank objectID="{15EF1495-4D7B-4A30-A6D5-1B5CB2314A78}" verb="New">
<extensions>
<symbolCoord>
<X>0.86176248</X><Y>0.7434433</Y>
</symbolCoord>
</extensions>
<mapLocation>
<coord>
<X>0.50187386</X><Y>0.8680783</Y>
</coord>
</mapLocation>
<facilityID>MXFMR070</facilityID>
<sectionID>MXFMR070</sectionID>
<parentSectionID name="07xx (X0X)" noun="substation" objectID="{16A743D9-AAA3-4187-94BF-77F63E25E5A6}"/>
<phaseCode>ABC</phaseCode>
<wdgCode>D-Y</wdgCode>
<vInput>67.0000535137532</vInput>
<vOut>7.19999980926514</vOut>
<tertVolts>0</tertVolts>
<tertChild name="MXFMR070" noun="transformerBank" objectID="{15EF1495-4D7B-4A30-A6D5-1B5CB2314A78}"/>
<vOutNom>7.19999980926514</vOutNom>
<vOutNomTertiary>0</vOutNomTertiary>
<transformerList>
<transformer objectID="{15EF1495-4D7B-4A30-A6D5-1B5CB2314A78}-ABC">
<kva>4750</kva>
<phases>3</phases>
<phase>ABC</phase>
<impedance>10.7299995422363</impedance>
<nLLoss>0</nLLoss>
<eaEquipment>519-X0X070</eaEquipment>
</transformer>
</transformerList>
</transformerBank>
<ohPrimaryLine objectID="{21EC1A14-276F-48EB-8955-80C0EF921CEB}" verb="New">
<extensions>
<note>LINE IS NOT HOT, JUMPERS CUT, NO FUSES ON POLE</note>
</extensions>
<complexLine>
<coord>
<X>0.37190654</X><Y>0.9178412</Y>
</coord>
<coord>
<X>0.78998409</X><Y>0.5761478</Y>
</coord>
</complexLine>
<sectionID>OH19244</sectionID>
<parentSectionID name="ROOT" noun="substation" objectID="{00000001-0000-0000-0000-000000000000}"/>
<phaseCode>A</phaseCode>
<conductorList>
<conductor>
<conductorType>#4 ACSR 6/1</conductorType>
<phase>A</phase>
</conductor>
</conductorList>
<condN>#4 ACSR 6/1</condN>
<condLength>415.600311279297</condLength>
<constr>SystemCnstDefault</constr>
<operVolt>7.19999980926514</operVolt>
</ohPrimaryLine>
<ohPrimaryLine objectID="{69B06530-99C3-4056-8929-D6E5583911AE}" verb="New">
<complexLine>
<coord>
<X>0.78998409</X><Y>0.5761478</Y>
</coord>
<coord>
<X>0.00206882</X><Y>0.0113029</Y>
</coord>
</complexLine>
<sectionID>OH19245</sectionID>
<parentSectionID name="OH19244" noun="ohPrimaryLine" objectID="{21EC1A14-276F-48EB-8955-80C0EF921CEB}"/>
<phaseCode>A</phaseCode>
<conductorList>
<conductor>
<conductorType>#4 ACSR 6/1</conductorType>
<phase>A</phase>
</conductor>
</conductorList>
<condN>#4 ACSR 6/1</condN>
<condLength>422.347900390625</condLength>
<constr>SystemCnstDefault</constr>
<operVolt>7.19999980926514</operVolt>
</ohPrimaryLine>
</MultiSpeak>
</MultiSpeakMsgHeader>
The quickest way is to use 'lxml.objectify': 最快的方法是使用“ lxml.objectify”:
from lxml import objectify
o= objectify.fromstring(xml)
ref: http://lxml.de/objectify.html#element-access-through-object-attributes 参考: http : //lxml.de/objectify.html#element-access-through-object-attributes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.