0

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?

3
  • Have you tried reading the elementtreee tutorial in the Python documentation? Commented Nov 5, 2018 at 21:58
  • @barny I edited the way my xml looks. and yes I did read. but I couldnt recreate any of those functions on my file. Commented Nov 5, 2018 at 22:00
  • If you look at the xpath support you can see that root.findall(‘./graph’) should find all the graph nodes and then for each of these, findall(‘./node’) should get you each node. Read the tutorial and try it out with the provided example, try different things. Commented Nov 5, 2018 at 22:10

1 Answer 1

1

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"))

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.