Parsing soap/xml result

Dave Angel davea at davea.name
Tue Jun 25 22:34:25 EDT 2013


On 06/25/2013 06:28 PM, miguel olivares varela wrote:
>
> I try to parse a soap/xml answer like:
>
> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>     <soapenv:Body>
>        <ns1:giftPkgResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://192.168.2.135:8490/gift-ws/services/SRV_GIFT_PKG">
>           <giftPkgReturn soapenc:arrayType="xsd:string[2]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
>              <giftPkgReturn> xsi:type="xsd:string">0</giftPkgReturn">
>              <giftPkgReturn> xsi:type="xsd:string">OK</giftPkgReturn> xsi:type="xsd:string">
>           </giftPkgReturn>
>        </ns1:giftPkgResponse>
>     </soapenv:Body>
> </soapenv:Envelope>
>
>
> here is my code
> ---------------------------------------------------------------------------
> import xml.etree.ElementTree as ET
> import re
>
> def parse(answer):
>      print"\nANSWER<<", answer
>      try:
>          tree = ET.fromstring(answer)
>          result = {}
>          for item in tree.getiterator():
>              if item.tag in ['giftPkgReturn', 'giftPkgReturn']:
>                  result[item.tag] = item.text
>
>          print "get<<%s" % result.get('status', None)
>          resp1 = result.get('giftPkgReturn', None)
>          resp2 = result.get('giftPkgReturn', None)
>
>          if (resp1 == "0" and resp2 == "OK"):
>              logger.info("Successful")
>              print "OK:"
>              return 0
>          else:
>              logger.info("Unsuccessful")
>              return -1
>
>       except Exception, Err:
>              print "\nERROR <<", str(Err)
>              return -1
>
> ----------------------------------------------------------------------------------------
>
> I got the error, i'm no a xml expert but it seems than the answser does not look like a pure xml.  i also tried with lxml instead of xml library but the result is te same
>
> xml lib
> ERROR << not well-formed (invalid token): line 5, column 77
>
> lxml
> ERROR << expected '>', line 5, column 37
>
>
> could you help me to correctly parse the answer please?
>
> regards,
> Miguel
>


You don't show the whole error, but I'd guess the error is being 
triggered in the line:          tree = ET.fromstring(answer)

None of these libraries help when fed ill-formed xml.

The line:
<giftPkgReturn> xsi:type="xsd:string">0</giftPkgReturn">

is just bogus.  I believe that line would be valid if you remove the 
first ">" symbol in the line.  But that's just my mental parser working, 
and I didn't try to feed it to a real xml library.  Note the next line 
has the same problem, and there could well be other problems in there.

The fix?  Get the author of the "the answer" to generate it correctly.

-- 
DaveA



More information about the Python-list mailing list