From xml data to python code to run

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ralfbrand50@gmail.com

    #1

    From xml data to python code to run

    Hello,

    I've got the following question:

    Is it possible to run data that's from an xml file as python code, by
    this I mean the following.

    In a xml file there's the following data

    <code><![CDATA[self.wTree.sign al_autoconnect( dic)]]></code>

    Now I want to extract the self.wTree.sign al_autoconnect( dic)* from the
    xml file and make it possible that Python runs this, instead of having
    a python file were with the hardcoded self.wTre.. in it.

    *this could my any sort of data, single or multiple lines, even whole
    def's

    Kind regards,

    Ralf Brand
    the Netherlands

  • Fredrik Lundh

    #2
    Re: From xml data to python code to run

    ralfbrand50@gma il.com wrote:
    Is it possible to run data that's from an xml file as python code, by
    this I mean the following.
    >
    In a xml file there's the following data
    >
    <code><![CDATA[self.wTree.sign al_autoconnect( dic)]]></code>
    >
    Now I want to extract the self.wTree.sign al_autoconnect( dic)* from the
    xml file and make it possible that Python runs this, instead of having
    a python file were with the hardcoded self.wTre.. in it.
    from elementtree import ElementTree as ET

    text = """<doc><code>< ![CDATA[object.method(' hello')]]></code></doc>"""

    doc = ET.fromstring(t ext)

    class Object:
    def method(self, value):
    print value

    # populate the namespace
    namespace = {
    "object": Object()
    }

    # execute all code fragments
    for code_elem in doc.findall(".//code"):
    exec code_elem.text in namespace

    </F>



    Comment

    Working...