Sorry if this is duplicate but I tried digging the Internet and did not really find something relevant.
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. The tags come with their ns. Here is a sample TAG, parsed and prettified with 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?
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':
from lxml import objectify
o= objectify.fromstring(xml)
ref: http://lxml.de/objectify.html#element-access-through-object-attributes
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.