def startElementNS(self, name, qname, attrs): prefix, uri, local_name = qname.getPrefix( ) or None, qname.getNamespaceURI() or None, qname.getLocalPart() e = Element(local_name, prefix=prefix, namespace=uri, parent=self._current_el) while attrs.hasNext(): attr = attrs.next() qname = attr.getName() prefix, uri, local_name = qname.getPrefix(), qname.getNamespaceURI( ), qname.getLocalPart() Attribute(local_name, attr.getValue(), prefix or None, uri or None, e) self._current_el = e self._current_level = self._current_level + 1
def startElementNS(self, name, qname, attrs): #print "$%s%s: %f" % (" " * self._current_level, name, time()) uri, local_name = name prefix = None if uri and uri in self._current_context: prefix = self._current_context[uri] #print "$$%s%s: %f" % (" " * self._current_level, name, time()) e = Element(local_name, prefix=prefix, namespace=uri, parent=self._current_el) #print "$$$%s%s: %f" % (" " * self._current_level, name, time()) for name, value in iter(attrs.items()): (namespace, local_name) = name qname = attrs.getQNameByName(name) prefix = self._split_qname(qname)[0] Attribute(local_name, value, prefix, namespace, e) #print "$$$$%s%s: %f" % (" " * self._current_level, name, time()) self._current_el = e self._current_level = self._current_level + 1
def __deserialize_fragment(self, current, parent): if current.attributes: for key in iter(current.attributes.keys()): attr = current.attributes[key] Attribute(attr.localName, attr.value, attr.prefix, attr.namespaceURI, parent) children_num = len(current.childNodes) children = iter(current.childNodes) for child in children: nt = child.nodeType if nt == xd.Node.TEXT_NODE: data = escape(child.data) if children_num == 1: parent.xml_text = data else: parent.xml_children.append(data) elif nt == xd.Node.CDATA_SECTION_NODE: parent.as_cdata = True data = child.data if children_num == 1: parent.xml_text = data else: parent.xml_children.append(data) elif nt == xd.Node.COMMENT_NODE: Comment(data=unicode(child.data), parent=parent) elif nt == xd.Node.PROCESSING_INSTRUCTION_NODE: PI(target=unicode(child.target), data=unicode(child.data), parent=parent) elif nt == xd.Node.ELEMENT_NODE: element = Element(name=child.localName, prefix=child.prefix, namespace=child.namespaceURI, parent=parent) self.__deserialize_fragment(child, element)