XML kokeiluja

Moi

XML filen parserointia harjoittelen Pythonilla.

NetistÀ löytyneeseen mallin yritÀn lisÀtÀ tietoa
http://code.activestate.com/recipes/577727-python-xml-parsing/

Ohjelma tulostaa ensin jokaisen noden erikseen. TÀmÀ toimii myös lisÀÀmillÀni tiedoilla.
Sitten yritÀn parseroida datan ulos jokaisesta nodesta.
AlkuperÀisessÀ esimerkissÀ kÀytetyt nimet ja iÀt saa erotettua XML-koodista

Ongelmana on, etten saa kaikista lisÀÀmÀstÀni nodeista pelkkÀÀ dataa irti ja printattua. Osa jÀÀ kokonaan piiloon, osa aiheuttaa ylivuodon ja kaatumisen (ne rivit on kommentoitu)

XML-filen tarkistin osoiteessa
w3schools.com/xml/xml_validator.asp

MikÀ mÀttÀÀ parserointiharjoituksessani?

[code]# -- coding: utf-8 --"""

from xml.dom import minidom

fsock = open(‘Ruuvipenkki.xml’)
xmldoc = minidom.parse(fsock)
#print (xmldoc.toxml())
#print (’\n’)

grammarNode = xmldoc.firstChild
grammarNode.childNodes

#NÀmÀ printtaukset nÀkyvÀt oikein
print ("*************** Print Child Node 1 ")
print (grammarNode.childNodes[1].toxml() )
print ("
Print Child Node 3 ")
print (grammarNode.childNodes[3].toxml())
print ("
Print Child Node 5 ")
print (grammarNode.childNodes[5].toxml())
print ("
Print Child Node 7 ***************")
print (grammarNode.childNodes[7].toxml())

print ("*************** Print Child Node 9 ***************")
print (grammarNode.childNodes[9].toxml())

print ("*************** Print Child Node 11 ***************")
print (grammarNode.childNodes[11].toxml())

#Parseroidaan dataa
print ("\n**************** NÀytÀ Data ******************** ")
#CHILD 1 TOIMII (esimerkin tietoa)
refNode = grammarNode.childNodes[1]
print (“child Node 1”)

pNode = refNode.childNodes[1]
print (“Nimi:”+ pNode.firstChild.data )
pNode = refNode.childNodes[3]
print (“IkĂ€:”+ pNode.firstChild.data )
pNode = refNode.childNodes[5]
print (“Vuosi:”+ pNode.firstChild.data )

print ("***************")

#CHILD 3 TOIMII
refNode = grammarNode.childNodes[3]
print (“child Node 3”)

pNode = refNode.childNodes[1]
print (“Nimi:”+ pNode.firstChild.data )
pNode = refNode.childNodes[3]
print (“IkĂ€:”+ pNode.firstChild.data )
pNode = refNode.childNodes[5]
print (“Vuosi:”+ pNode.firstChild.data )
print ("***************")

#CHILD 5 Tomii
refNode = grammarNode.childNodes[5]
print (“child Node 5”)
pNode = refNode.childNodes[1]
print (“stepnumber:”+ pNode.firstChild.data )

pNode = refNode.childNodes[3]
print (“modedisplay:”+ pNode.firstChild.data )

pNode = refNode.childNodes[5]
print (“output:”+ pNode.firstChild.data )
print ("***************")

#CHILD 7 ARVOT EI TULOSTU ?? .NÀmÀ lisÀsin esimerkkiin
refNode = grammarNode.childNodes[7]
print (“child Node7”)
pNode = refNode.childNodes[1]
print (“stepnumber:”+ pNode.firstChild.data )

pNode = refNode.childNodes[3]
print (“modedisplay:”+ pNode.firstChild.data )

#pNode = refNode.childNodes[5] # LIST INDE OUT OF RANGE
print (“output:”+ pNode.firstChild.data )
print ("***************")

#CHILD 9 Toimii
refNode = grammarNode.childNodes[9]
print (“child Node 9”)
pNode = refNode.childNodes[1]
print (“Steppi:”+ pNode.firstChild.data )

pNode = refNode.childNodes[3]
print (“Tapa:”+ pNode.firstChild.data )

pNode = refNode.childNodes[5]
print (“Tulos:”+ pNode.firstChild.data )

pNode = refNode.childNodes[7]
print (“Tulos:”+ pNode.firstChild.data )
print ("***************")

#CHILD 11 Edes eka arvo ei tulostu, Kaksi alinta kommentoitu ettei vuoda yli
refNode = grammarNode.childNodes[11]
print (“child Node 11”)
pNode = refNode.childNodes[1]
print (“Steppi:”+ pNode.firstChild.data )

#pNode = refNode.childNodes[3] List index out of range
print (“Tapa:”+ pNode.firstChild.data )

#pNode = refNode.childNodes[5] #List index out of range
print (“Tulos:”+ pNode.firstChild.data )

print ("***************")
[/code]

<?xml version="1.0" ?> <test> <value> <name> Abhijeet Vaidya </name> <age> 21 </age> <year> 1990 </year> </value> <Midlevalue> <name> Keerthan Pai </name> <age> 21 </age> <year> 1990 </year> </Midlevalue> <Endvalue> <name> Krishnaraj </name> <age> 21 </age> <year> 1990 </year> </Endvalue> <teststeps> <step> <steppi>1</steppi> <tapa>L0</tapa> <tulos>543.184 mV</tulos> </step> <step> <stepnumber>2</stepnumber> <modedisplay>L1</modedisplay> <output>543.184 mV</output> </step> </teststeps> <commands> <command> Step #1 L0, 11 </command> <command> Step #2 L1, 22 </command> <command> Step #3 L2, 33 </command> <command> Step #4 L3, 44 </command> </commands> <instruments> <instrument> <name> Mittalaite </name> <date> 1.1.1970 </date> <instrserial> 1234 </instrserial> </instrument> </instruments> </test>