Xmlparser: new class that just store one node, futher nodes will be parsed

This commit is contained in:
nemunaire 2015-11-22 14:46:34 +01:00
parent 1e29061bc9
commit d59f629dd9

View File

@ -14,44 +14,24 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
class GenericNode: class ParsingNode:
def __init__(self, tag, **kwargs): """Allow any kind of subtags, just keep parsed ones
"""
def __init__(self, tag=None, **kwargs):
self.tag = tag self.tag = tag
self.attrs = kwargs self.attrs = kwargs
self.content = "" self.content = ""
self.children = [] self.children = []
self._cur = None
self._deep_cur = 0
def startElement(self, name, attrs):
if self._cur is None:
self._cur = GenericNode(name, **attrs)
self._deep_cur = 0
else:
self._deep_cur += 1
self._cur.startElement(name, attrs)
return True
def characters(self, content): def characters(self, content):
if self._cur is None: self.content += content
self.content += content
else:
self._cur.characters(content)
def endElement(self, name): def addChild(self, name, child):
if name is None: self.children.append(child)
return
if self._deep_cur:
self._deep_cur -= 1
self._cur.endElement(name)
else:
self.children.append(self._cur)
self._cur = None
return True return True
@ -71,3 +51,44 @@ class GenericNode:
def __contains__(self, item): def __contains__(self, item):
return item in self.attrs return item in self.attrs
class GenericNode(ParsingNode):
"""Consider all subtags as dictionnary
"""
def __init__(self, tag, **kwargs):
super().__init__(tag, **kwargs)
self._cur = None
self._deep_cur = 0
def startElement(self, name, attrs):
if self._cur is None:
self._cur = GenericNode(name, **attrs)
self._deep_cur = 0
else:
self._deep_cur += 1
self._cur.startElement(name, attrs)
return True
def characters(self, content):
if self._cur is None:
super().characters(content)
else:
self._cur.characters(content)
def endElement(self, name):
if name is None:
return
if self._deep_cur:
self._deep_cur -= 1
self._cur.endElement(name)
else:
self.children.append(self._cur)
self._cur = None
return True