Rasika
Rasika

Reputation: 507

Parsing and editing XML file in python

I have a XML file as follows:

    <?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 http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
<key id="labelV" for="node" attr.name="labelV" attr.type="string"/>
<key id="Count" for="node" attr.name="Count" attr.type="double"/>
<key id="URI" for="node" attr.name="URI" attr.type="string"/>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"/>
<graph id="G" edgedefault="directed">
  <node id="4096">
    <data key="labelV">v</data>
    <data key="Count">1.0</data>
    <data key="URI">http://www.guavus.com/rflx/ont/c?ln=en&amp;v=1.0&amp;cust=comcast&amp;prj=x1&amp;id=63a9f0ea7bb98050796b649e85481845&amp;norm=n</data>
  </node>
  <node id="4104">
    <data key="labelV">v</data>
    <data key="Count">0.1111111111111111</data>
    <data key="URI">http://www.guavus.com/rflx/ont/l?ln=en&amp;v=1.0&amp;cust=comcast&amp;prj=x1&amp;id=c01ed2b3bffa35c9c2d2c3c723f18bdb&amp;norm=n</data> 
  </node>...

I want to add more data elements to the node element. I am not able to reach to the node element.

tree = ET.parse("ner.xml")
root = tree.getroot()
print(root)

I get

<Element '{http://graphml.graphdrawing.org/xmlns}graphml' at 0x10e015188>

and root.findall('graph') return empty. why is that? Can anyone help me with this?

Upvotes: 0

Views: 158

Answers (1)

Lie Ryan
Lie Ryan

Reputation: 64953

Your XML have a namespace. You need to either declare the namespace you want to use when parsing it (e.g. ET.register_namespace("gml", "http://graphml.graphdrawing.org/xmlns"); root.find_all("gml:graph")) or use the fully qualified name when working with the elements (e.g. root.find_all("{http://graphml.graphdrawing.org/xmlns}graph"))

Upvotes: 1

Related Questions