无法用elemen分析graphml文件

2024-09-29 17:21:35 发布

您现在位置:Python中文网/ 问答频道 /正文

xml

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <graph id="G" edgedefault="undirected">
    <node id="n0"/>
    <node id="n1"/>
    <edge id="e1" source="n0" target="n1"/>
  </graph>
</graphml>

python代码

^{pr2}$

如果我从graphml标记中删除属性,那么它就可以工作了,返回元素


Tags: orgidnodehttpversionxmlutfencoding
1条回答
网友
1楼 · 发布于 2024-09-29 17:21:35

因为XML文档中没有简单的graph元素,所以得到了一个空列表。您的文档有一个默认的XML名称空间(属于http://graphml.graphdrawing.org/xmlns),因此文档中没有显式名称空间前缀的任何元素都位于该名称空间中。在

这意味着在请求元素时,需要提供名称空间信息和标记名。例如:

>>> tree.findall('{http://graphml.graphdrawing.org/xmlns}graph')
[<Element {http://graphml.graphdrawing.org/xmlns}graph at 0x7f2f3e3cf5f0>]
>>> 

LXML文档有一个关于working with namespaces的部分。在

相关问题 更多 >

    热门问题