From 69d0e1f7d589c32a3b47361bbd6b0189acd6f385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C2=B3?= Date: Fri, 11 Jan 2019 16:57:24 +0100 Subject: [PATCH 1/3] parse data tags of nodes correctly getElementsByName() will get data-tags of all descendants not only of direct children. This implementation only searches for data-tags of direct children. --- pygraphml/graphml_parser.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pygraphml/graphml_parser.py b/pygraphml/graphml_parser.py index bca02be..c641c37 100644 --- a/pygraphml/graphml_parser.py +++ b/pygraphml/graphml_parser.py @@ -5,6 +5,7 @@ from __future__ import absolute_import from __future__ import print_function +import xml.dom from xml.dom import minidom from . import Graph @@ -143,12 +144,12 @@ def parse(self, fname): # Get nodes for node in graph.getElementsByTagName("node"): n = g.add_node(id=node.getAttribute('id')) - - for attr in node.getElementsByTagName("data"): - if attr.firstChild: - n[attr.getAttribute("key")] = attr.firstChild.data - else: - n[attr.getAttribute("key")] = "" + for attr in node.childNodes: + if attr.nodeType == xml.dom.Node.ELEMENT_NODE and attr.tagName == "data": + if attr.firstChild: + n[attr.getAttribute("key")] = attr.firstChild.data + else: + n[attr.getAttribute("key")] = "" # Get edges for edge in graph.getElementsByTagName("edge"): From 2ecb07aea6066129f6b2788554654c23cf27e3b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C2=B3?= Date: Mon, 14 Jan 2019 11:46:24 +0100 Subject: [PATCH 2/3] parse data tags of edges correctly getElementsByName() will get data-tags of all descendants not only of direct children. This implementation only searches for data-tags of direct children. --- pygraphml/graphml_parser.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pygraphml/graphml_parser.py b/pygraphml/graphml_parser.py index c641c37..800b503 100644 --- a/pygraphml/graphml_parser.py +++ b/pygraphml/graphml_parser.py @@ -128,7 +128,7 @@ def parse(self, fname): """ g = None - with open( fname, 'r') as f: + with open(fname, 'r') as f: dom = minidom.parse(f) root = dom.getElementsByTagName("graphml")[0] graph = root.getElementsByTagName("graph")[0] @@ -144,12 +144,12 @@ def parse(self, fname): # Get nodes for node in graph.getElementsByTagName("node"): n = g.add_node(id=node.getAttribute('id')) - for attr in node.childNodes: - if attr.nodeType == xml.dom.Node.ELEMENT_NODE and attr.tagName == "data": - if attr.firstChild: - n[attr.getAttribute("key")] = attr.firstChild.data + for child in node.childNodes: + if child.nodeType == xml.dom.Node.ELEMENT_NODE and child.tagName == "data": + if child.firstChild: + n[child.getAttribute("key")] = child.firstChild.data else: - n[attr.getAttribute("key")] = "" + n[child.getAttribute("key")] = "" # Get edges for edge in graph.getElementsByTagName("edge"): @@ -158,12 +158,12 @@ def parse(self, fname): # source/target attributes refer to IDs: http://graphml.graphdrawing.org/xmlns/1.1/graphml-structure.xsd e = g.add_edge_by_id(source, dest) - - for attr in edge.getElementsByTagName("data"): - if attr.firstChild: - e[attr.getAttribute("key")] = attr.firstChild.data - else: - e[attr.getAttribute("key")] = "" + for child in edge.childNodes: + if child.nodeType == xml.dom.Node.ELEMENT_NODE and child.tagName == "data": + if child.firstChild: + n[child.getAttribute("key")] = child.firstChild.data + else: + n[child.getAttribute("key")] = "" return g From 38348f9b3962e2bdd2f9c380c88a2a2a4896febc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C2=B3?= Date: Tue, 15 Jan 2019 15:51:06 +0100 Subject: [PATCH 3/3] fix copy paste error --- pygraphml/graphml_parser.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pygraphml/graphml_parser.py b/pygraphml/graphml_parser.py index 800b503..110e959 100644 --- a/pygraphml/graphml_parser.py +++ b/pygraphml/graphml_parser.py @@ -64,7 +64,8 @@ def write(self, graph, fname=None): edge.setAttribute('source', e.node1['label']) edge.setAttribute('target', e.node2['label']) if e.directed() != graph.directed: - edge.setAttribute('directed', 'true' if e.directed() else 'false') + edge.setAttribute( + 'directed', 'true' if e.directed() else 'false') for a in e.attributes(): if e != 'label': data = doc.createElement('data') @@ -147,7 +148,8 @@ def parse(self, fname): for child in node.childNodes: if child.nodeType == xml.dom.Node.ELEMENT_NODE and child.tagName == "data": if child.firstChild: - n[child.getAttribute("key")] = child.firstChild.data + n[child.getAttribute( + "key")] = child.firstChild.data else: n[child.getAttribute("key")] = "" @@ -161,9 +163,10 @@ def parse(self, fname): for child in edge.childNodes: if child.nodeType == xml.dom.Node.ELEMENT_NODE and child.tagName == "data": if child.firstChild: - n[child.getAttribute("key")] = child.firstChild.data + e[child.getAttribute( + "key")] = child.firstChild.data else: - n[child.getAttribute("key")] = "" + e[child.getAttribute("key")] = "" return g