1

嗨,我有一个简单的 graphML 文件,我想从 GraphML 中删除节点标记并将其保存在另一个 GraphML 文件中。下面给出的 GraphML 大小为 3GB 是示例。

输入文件 :

<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
    <key id="weight" for="edge" attr.name="weight" attr.type="string"></key>
    <graph id="G" edgedefault="directed">
        <node id="1"></node>
        <node id="2">
        </node>
        <node id="3">
        </node>
        <node id="4">
        </node>
        <node id="5">
        </node>
        <edge id="6" source="1" target="2">
            <data key="weight">3</data>
        </edge>
        <edge id="7" source="2" target="4">
            <data key="weight">1</data>
        </edge>
        <edge id="8" source="2" target="3">
            <data key="weight">9</data>
        </edge>
    </graph>
</graphml>

所需输出:

<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
    <key id="weight" for="edge" attr.name="weight" attr.type="string"></key>
    <graph id="G" edgedefault="directed">
        <edge id="6" source="1" target="2">
            <data key="weight">3</data>
        </edge>
        <edge id="7" source="2" target="4">
            <data key="weight">1</data>
        </edge>
        <edge id="8" source="2" target="3">
            <data key="weight">9</data>
        </edge>
    </graph>
</graphml>

有什么方法可以做到这一点吗?

4

2 回答 2

1

在阅读了一些链接之后,我想出了迭代解析的解决方案。顺便说一句,我无法弄清楚简单解析和迭代解析在 RAM 使用方面的区别。

重要链接:
- http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
-使用 lxml 和 iterparse() 解析一个大 (+- 1Gb) XML 文件

代码 :

导入 lxml.etree 作为 et

graphml = {  
   "graph": "{http://graphml.graphdrawing.org/xmlns}graph",  
   "node": "{http://graphml.graphdrawing.org/xmlns}node",  
   "edge": "{http://graphml.graphdrawing.org/xmlns}edge",  
   "data": "{http://graphml.graphdrawing.org/xmlns}data",  
   "weight": "{http://graphml.graphdrawing.org/xmlns}data[@key='weight']",  
   "edgeid": "{http://graphml.graphdrawing.org/xmlns}data[@key='edgeid']"  
}



for event, elem in et.iterparse("/data/sample.graphml",tag=graphml.get("edge"), events = ('end', )):  
    print(et.tostring(elem))
    elem.clear()
    while elem.getprevious() is not None:
        del elem.getparent()[0]
于 2017-01-20T11:11:07.547 回答
1

有一个python模块来处理graphml。奇怪的是,文档没有remove或没有delete功能。

由于 graphml 是 xml 标记,您可以改用 xml 模块。我使用过xmltodict并且非常喜欢它。该模块允许您将 xml 代码加载到 python 对象。修改对象后,可以将其保存回xml。

如果data是包含 xml 的字符串:

data_object=xmltodict.parse(data)
del data_object["graphml"]["graph"]["node"]
xmltodict.unparse(data_object, pretty=True)

这将删除node条目,unparse 将返回一个带有 xml 的字符串。

如果 xml 的结构变得更复杂,您需要在data_object. 但这不应该是一个问题,它只是一个有序的字典。

另一个问题可能是 xml 的大小。3GB很多。xmltodict 确实支持大文件的流模式,但这是我从未使用过的。

于 2017-01-19T07:31:22.153 回答