read library example etree dict create python xml file xml-parsing elementtree

library - python xml to dict



la raíz del archivo xml está dando NINGUNO ¿por qué? (1)

El siguiente código es suficiente. No necesita abrir el archivo al comienzo. ET.parse lo hará por usted si proporciona la ruta correcta.

En su código, importe la biblioteca como ET y luego reasigne la misma variable abriendo el archivo. Entonces ET.parse() refiere a su objeto de archivo no a su biblioteca elementtree .

import xml.etree.ElementTree as ET tree= ET.parse(''note.xml'') root= tree.getroot() print(root.tag) # to print root

from elementtree import ElementTree as ET tree= ET.parse(r''N:/myinternwork/files xml of bus systems/testonieeebus.xml'',''r'') root= tree.getroot() print(root)

ahora el error está en la salida, ya que no está dando ninguna

<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="cooking"> <author>Giada De Laurentiis</author> </book> </bookstore>