[BangPypers] XML manipulation using ElementTree

Keith Chapman keith.chap at hotmail.com
Fri May 7 13:02:57 CEST 2010


Hi,
    I have two javascript functions. It has been written to retrieve information from the incoming XML message and return a new XML. My javascript functions are working perfectly fine. What I need to do is to write the same logic in python. Therefore I have used ElementTree to do my work. But it's giving errors. I am very new to Python programming language and I'm finding it difficult to understand some stuff related to Python. If someone can point me what's I'm doing wrong, I would be greateful? 
    


// javascript functions
function transformRequest(mc) {
     var symbol = mc.getPayloadXML()..*::Code.toString();
     mc.setPayloadXML(
        <m:getQuote xmlns:m="http://services.samples/xsd">
           <m:request>
              <m:symbol>{symbol}</m:symbol>
           </m:request>
        </m:getQuote>);
  }

  function transformResponse(mc) {
     var symbol = mc.getPayloadXML()..*::symbol.toString();
     var price = mc.getPayloadXML()..*::last.toString();
     mc.setPayloadXML(
        <m:CheckPriceResponse xmlns:m="http://www.apache-synapse.org/test">
   <m:Code>{symbol}</m:Code>
   <m:Price>{price}</m:Price>
        </m:CheckPriceResponse>);
  }

# Python functions
from elementtree.ElementTree as ET import Element, SubElement, QName

def transformRequest(mc):
    tree = ET.parse(mc)
    symbol = tree.findtext("CheckPriceRequest/Code")
    tree.findtext("head/title")

    request = Element(mc)
    request.set(
        "<m:getQuote xmlns:m="http://services.samples/xsd">
           <m:request>
              <m:symbol>" + symbol + "</m:symbol>
           </m:request>
        </m:getQuote>"
        )
    return request

def transformResponse(mc):
    tree = ET.parse(mc)
    symbol = tree.findtext("CheckPriceResponse/symbol")
    price = tree.findtext("CheckPriceResponse/last")
    tree.findtext("head/title")

    response = Element(mc)
    response.set(
        "<m:CheckPriceResponse xmlns:m="http://www.apache-synapse.org/test">
         <m:Code>" + symbol + "</m:Code>
         <m:Price>" + price + "</m:Price>
         </m:CheckPriceResponse>"
        )
    return response


Thanks
 		 	   		  
_________________________________________________________________
Hotmail: Free, trusted and rich email service.
https://signup.live.com/signup.aspx?id=60969


More information about the BangPypers mailing list