[BangPypers] XML manipulation using ElementTree

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

    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();
        <m:getQuote xmlns:m="http://services.samples/xsd">

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

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

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

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

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

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

Hotmail: Free, trusted and rich email service.

More information about the BangPypers mailing list