2015-09-10 14 views
5

Ho questo risultato stringa XML e ho bisogno di ottenere i valori tra i tag. Ma il tipo di dati dell'XML è una stringa.Analizza una stringa XML in Python

final = " <Table><Claimable>false</Claimable><MinorRev>80601</MinorRev><Operation>530600 ION MILL</Operation><HTNum>162</HTNum><WaferEC>80318</WaferEC><HolderType>HACARR</HolderType><Job>167187008</Job></Table> 

    <Table><Claimable>false</Claimable><MinorRev>71115</MinorRev><Operation>530600 ION MILL</Operation><Experiment>6794</Experiment><HTNum>162</HTNum><WaferEC>71105</WaferEC><HolderType>HACARR</HolderType><Job>16799006</Job></Table> " 

Questo è il mio codice di esempio

root = ET.fromstring(final) 
print root 

E questo è l'errore che sto ricevendo:

xml.parsers.expat.ExpatError: The markup in the document following the root element must be well-formed. 

Ive provato ad utilizzare ET.fromstring. Ma senza fortuna.

+0

Qual è 'ET' esattamente nel codice? – har07

+0

Per favore, dai il tuo esempio di codice, che aiuterà gli altri a capire cosa stai provando? – Nilesh

+0

Ho modificato la domanda @ har07 – essramos

risposta

13

tuo XML non è valido . Deve avere esattamente un elemento di livello superiore. From Wikipedia:

Each XML document has exactly one single root element. It encloses all the other elements and is therefore the sole parent element to all the other elements. ROOT elements are also called PARENT elements.

Cercare di racchiudere entro tag aggiuntivo (ad es Tables) e di interpretare da ET:

xmlData = '''<Tables> 
<Table><Claimable>false</Claimable><MinorRev>80601</MinorRev><Operation>530600 ION MILL</Operation><HTNum>162</HTNum><WaferEC>80318</WaferEC><HolderType>HACARR</HolderType><Job>167187008</Job></Table> 
<Table><Claimable>false</Claimable><MinorRev>71115</MinorRev><Operation>530600 ION MILL</Operation><Experiment>6794</Experiment><HTNum>162</HTNum><WaferEC>71105</WaferEC><HolderType>HACARR</HolderType><Job>16799006</Job></Table> 
</Tables> 
''' 

import xml.etree.ElementTree as ET 
xml = ET.fromstring(xmlData) 

for table in xml.getiterator('Table'): 
    for child in table: 
     print child.tag, child.text 

Dal Python 2.7 getiterator('Table') dovrebbe essere sostituito con iter('Table'):

for table in xml.iter('Table'): 
    for child in table: 
     print child.tag, child.text 

Questo produce:

Claimable false 
MinorRev 80601 
Operation 530600 ION MILL 
HTNum 162 
WaferEC 80318 
HolderType HACARR 
Job 167187008 
Claimable false 
MinorRev 71115 
Operation 530600 ION MILL 
Experiment 6794 
HTNum 162 
WaferEC 71105 
HolderType HACARR 
Job 16799006 
+0

Risposta perfetta, anche se suggerisco all'OP di cambiare il nome del tag radice in qualcosa di più rilevante per l'OP rispetto a . –

+0

@BhargavRao: come suggerito, ho cambiato il tag in 'Tabelle'. –

+1

Grazie per questo cambiamento. Queste piccole cose sono effetti collaterali del lavoro nel settore. Saluti. –

2

Forse voi ha provato node.attrib, provare node.text invece di ottenere il valore stringa (vedi anche Parsing XML nella documentazione Python):

import xml.etree.ElementTree as ET 
xml_string = "<Table><Claimable>false</Claimable><MinorRev>80601</MinorRev><Operation>530600 ION MILL</Operation><HTNum>162</HTNum><WaferEC>80318</WaferEC><HolderType>HACARR</HolderType><Job>167187008</Job></Table>" 

root = ET.fromstring(xml_string) 

for child in root: 
    print child.tag, child.text 

Questo dovrebbe dare il

Claimable false 
MinorRev 80601 
Operation 530600 ION MILL 
HTNum 162 
WaferEC 80318 
HolderType HACARR 
Job 167187008 
+0

Questo è quello che ho provato in un primo momento, ma ho dovuto aggiungere un tag di livello superiore per farlo funzionare. Grazie! @adrianus – essramos