Пример #1
0
 def _get(self, keyList):
     printlc(D, "dprint -> " + str(keyList), self, "D")
     currentLevel = self.xmlDom.firstChild
     try:
         for key in keyList:
             # print currentLevel, type(currentLevel), key, type(key)
             if type(key) is int:
                 currentLevel = currentLevel[key]
             else:
                 if type(currentLevel) is NodeList:
                     currentLevel = currentLevel[0]
                 currentLevel = currentLevel.getElementsByTagName(key)
         if type(currentLevel) is NodeList and len(currentLevel) == 1:
             currentLevel = currentLevel[0]
     except TypeError, ex:
         pass
Пример #2
0
    def _createNodeDict(self, node):
        printlc(D, "_createNodeDict -> " + str(node), self, "D")
        if type(node) is NodeList:
            l = []
            for subnode in node:
                m = self._create(subnode)
                l.append({m[0]: m[1]})

            printlc(D, "_createNodeDict <- " + str(l), self, "D")
            return l
        else:
            m = self._create(node)
            printlc(D, "_createNodeDict <- " + str({m[0]: m[1]}), self, "D")
            return {m[0]: m[1]}
Пример #3
0
    def _create(self, node):
        if node.nodeType == node.TEXT_NODE:
            data = node.data.strip()
            if len(data) == 0:
                # printlc(D, "_create <-1 " + str(None), self,"D")
                return None
            printlc(D, "_create -> " + str(node), self, "D")
            printlc(D, "_create <-2 " + str(data), self, "D")
            return data
        printlc(D, "_create -> " + str(node), self, "D")

        childrenList = []
        for child in node.childNodes:
            childNode = self._create(child)
            if childNode is not None:
                if type(childNode) is unicode:
                    if childNode[0] == u"i":
                        try:
                            printlc(D, "_create <-3 " + str((node.tagName, int(childNode[1:]))), self, "D")
                            return (node.tagName, int(childNode[1:]))
                        except:
                            pass
                    elif childNode == u"bFalse":
                        printlc(D, "_create <-4 " + str((node.tagName, False)), self, "D")
                        return (node.tagName, False)
                    elif childNode == u"bTrue":
                        printlc(D, "_create <-5 " + str((node.tagName, True)), self, "D")
                        return (node.tagName, True)
                    printlc(D, "_create <-6 " + str((node.tagName, childNode)), self, "D")
                    return (node.tagName, childNode)
                else:
                    printlc(D, "_create append " + str((node.tagName, childNode)), self, "D")
                    childrenList.append((node.tagName, childNode))

        if node.hasAttributes():
            printlc(D, "_create " + str(node.attributes.keys()), self, "D")
            for key in node.attributes.keys():
                childrenList.append(("attr_" + key, ("attr_" + key, node.attributes.get(key).value)))

        children = {}
        for child in childrenList:
            if children.has_key(child[1][0]):
                if type(children[child[1][0]]) is not list:
                    children[child[1][0]] = [children[child[1][0]], child[1][1]]
                else:
                    children[child[1][0]].append(child[1][1])
            else:
                children[child[1][0]] = child[1][1]
        printlc(D, "_create <-7 " + str((node.tagName, children)), self, "D")
        return (node.tagName, children)
Пример #4
0
                if type(key) is int:
                    currentLevel = currentLevel[key]
                else:
                    if type(currentLevel) is NodeList:
                        currentLevel = currentLevel[0]
                    currentLevel = currentLevel.getElementsByTagName(key)
            if type(currentLevel) is NodeList and len(currentLevel) == 1:
                currentLevel = currentLevel[0]
        except TypeError, ex:
            pass
        except IndexError, ex:
            pass
        except Exception, ex:
            print ex, type(ex)

        printlc(D, "dprint <- " + str(currentLevel), self, "D")
        return currentLevel

        # http://code.activestate.com/recipes/577739-dict2xml/ (GPL3)

    def _build(self, father, structure):
        if type(structure) == dict:
            for k in structure:
                tag = self.xmlDom.createElement(k)
                father.appendChild(tag)
                self._build(tag, structure[k])

        elif type(structure) == list:
            grandFather = father.parentNode
            uncle = copy.deepcopy(father)
            for l in structure: