I am trying to parse a XML file with python for a school project.
To see if the prasing works I printed the values of the "lista_marfuri".
It shows the following error: xml.parsers.expat.ExpatError: XML declaration not well-formed: line 1, column 35
The XML code is:
<?xml version="1.0" encoding="UTF-8 standalone="yes"?>
<fapte>
<lista_marfuri>
<marfa>
<id> 1 </id>
<nume> grebla </nume>
<categorie> gradinarit </gradinarit>
<cantitate> 100 </cantitate>
<pret> 20 </pret>
</marfa>
<marfa>
<id> 2 </id>
<nume> sac 1kg ingrasamant </nume>
<categorie> gradinarit </gradinarit>
<cantitate> 300 </cantitate>
<pret> 30 </pret>
</marfa>
<marfa>
<id> 3 </id>
<nume> surubelnita </nume>
<categorie> general </gradinarit>
<cantitate> 200 </cantitate>
<pret> 5 </pret>
</marfa>
</lista_marfuri>
<lista_categorii>
...
</lista_categorii>
<lista_clienti>
...
</lista_clienti>
<lista_comenzi>
...
</lista_comenzi>
</fapte>
And the python code is:
import xml.dom.minidom
tree = xml.dom.minidom.parse('SBC.xml')
fapte = tree.documentElement
marfuri = fapte.getElementsByTagName('marfa')
for marfa in marfuri:
print(f"-- Marfa {marfa.getAttribute('id')} --")
nume = marfa.getElementByTagName('nume')[0].childNodes[0].nodeValue
categorie = marfa.getElementByTagName('categorie')[0].childNodes[0].nodeValue
cantitate = marfa.getElementByTagName('cantitate')[0].childNodes[0].nodeValue
pret = marfa.getElementByTagName('pret')[0].childNodes[0].nodeValue
print(f"Nume: {nume}")
print(f"Categorie: {categorie}")
print(f"Cantitate: {cantitate}")
print(f"Pret: {pret}")
encoding="UTF-8, you needencoding="UTF-8".<categorie>start tags require corresponding</categorie>end tags.getElementByTagNameshould begetElementsByTagName.