• Login or register

discuss.effbot.org

  • Popular
  • Recent
  • General Discussion for effbot.org

    Post comments not related to any specific article here. Comments about this commenting system can be posted here. Old and off-topic discussions will be removed regularly. /F

    1 point by effbot 11 months ago
    • 6 comments
  • 1 point by banaouas 5 months ago 0 children

    Hi all,
    I looked for clone element function but not found such tool:
    def CloneElment(fromElem, destRoot = None)
    fromElem is the element to clone
    destRoot is the container of the new element ;if None so the new element will be contained by the fromElem container
    here is a first implementation:


    def CloneElement(fromElem, destRoot = None):
    fromRoot = ET.ElementTree(fromElem).getroot()
    if destRoot == None:
    destRoot = fromRoot
    destElem = destRoot.makeelement(fromElem.tag, fromElem.attrib)
    destRoot.append(destElem)
    destElem.text = fromElem.text
    for e in fromElem.findall('*'):
    CloneElement(e, destElem)
    #

    Thanks for any advice.

    • link
    • reply
  • 1 point by MarcusSchorn 9 months ago 4 children

    Hello Fredrik,

    we work with ElementSoap and found out that namespace_parse does not work as reliable as ET.parse. ET.parse seems to handle namespaces much better than namespace_parse does ;-)

    I would suggest the following changes to the call function in ElementSOAP.py:

    ------- snippet -----------
    # *** changed - param 'parser' added
    def call(self, action, request, header=None, parser=None):
    ...
    # call the server
    try:
    response = self.__client.do_request(
    ET.tostring(envelope),
    extra_headers=[("SOAPAction", action)],
    parser=parser # *** changed
    )
    except HTTPError, v:
    if v[0] == 500:
    # might be a SOAP fault
    response = ET.parse(v[3]) # *** changed
    ...
    ------- snippet -----------

    Thanks a lot!

    Marcus

    • link
    • reply
    • 1 point by effbot 9 months ago 3 children

      What does "not as reliable" mean here? The reason ElementSoap uses a parser wrapper is that the standard parser doesn't preserve namespace prefixes, which are needed to resolve things like XSI types, fault codes, and other places where SOAP puts prefixes in parsed content.

      • link
      • reply
      • 1 point by MarcusSchorn 8 months ago 2 children

        It is just an oberservation.
        This is something we get as a result from a webservice:
        --------------------------------------------------------
        from elementsoap.ElementSOAP import namespace_parse
        import elementtree.ElementTree as ET

        xmltext = """<?xml version="1.0" encoding="ISO-8859-1" ?><ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"><ns0:Body><ns1:getMasterDataResponse xmlns:ns1="XeriAPI"><resultList command="getMasterData" version="1.0">
        <user>
        <title value="Jan"/>
        </user>
        <doctype>
        <title value="Richtlinie T"/>
        <title value="Formular"/>
        </doctype>
        <process>
        <title value="Controlling"/>
        </process>
        <department>
        <title value="Integral Development GmbH"/>
        </department>
        <category>
        <title value="Beschaffung / Purchase"/>
        </category>
        <responsibleUnit>
        <title value="Support"/>
        </responsibleUnit>
        <location>
        <title value="Stockholm"/>
        <title value="Paris"/>
        <title value="Hamburg"/>
        </location>
        </resultList></ns1:getMasterDataResponse></ns0:Body></ns0:Envelope>
        """
        class Myfile:
        isread = False
        def read(self, *args, **kw):
        if Myfile.isread:
        return ""
        Myfile.isread = True
        return xmltext

        try:
        res = ET.parse(Myfile())
        print "ET.parse worked:", res
        except:
        print "ET.parse error!"

        try:
        res = namespace_parse(Myfile())
        print "namespace_parse worked:", res
        except:
        print "namespace_parse error!"

        --------------------------------------------------------

        • link
        • reply
        • 1 point by effbot 8 months ago 1 child

          Thanks for the sample. I'll look into this as soon as I find the time.

          Update: Your sample code is setting a class attribute (Myfile.isread) instead of an instance attribute (self.isread). Since class attributes are shared between all instances of a class, the second Myfile instance will always return an empty string -- which causes the second parser to raise a "ParseError: no element found" exception. If I fix that, both parsers successfully parse that XML snippet.

          • link
          • reply
          • 1 point by MarcusSchorn 8 months ago 0 children

            Opps, you are right. I have originally used it in the running system. And cutted some XML away. I will check again.

            • link
            • reply
  • Widget
  • Recent Comments
  • Leaders
Powered by