2016-04-30 9 views
0

私はPythonには新しく、XMLで作業したことがないので、専門知識が不足しています。Python 3.4:XMLツリーを使って移動する

私はそれの内部XMLとかなり長いテキストファイルがあります:私は「場所」タグの下に「入所」タグと名の値の下の値を取得しようとしています

- - - - Some Text Until This Point — — — - - - 


<?xml version="1.0" encoding="UTF-8"?> 
<Record xmlns="http://www.south.org/> 
    <Patient> 
    <ID>4</ID> 
    <Status>Good</Status> 
    <Pain>8</Pain> 
    </Patient> 
    <Hospital> 
    <Name>South Center.</Name> 
    <Address>1234 Main Ave New York NY 4567 United States</Address> 
    <Phone>+1 (123) 456 7890</Phone> 
    <Email>[email protected]</Email> 
    </Hospital> 
    <Insurance> 
    <Name>Health First</Name> 
    <Phone>+1 (123) 456 7890</Phone> 
    </Insurance> 
    <Admitted> 
    <Date>2000-11-8t7:24:02</Date> 
    <Injury>Arm</Injury> 
    <Location>7</Location> 
    </Admitted> 
    <Place> 
    <Room> 
     <Number>28</Number> 
     <Wing>East</Wing> 
    <Name>John Smith</Name> 
    </Room> 
    </Place> 
</Record> 

- - - - - - - - - - - Some more Text - - - - - — - - - - - — - - - - 

をし、それらをローカル変数に保存します。私はこの質問が以下にリストされたものと非常に似ていることは知っていますが、私はまだそれを正しいものにすることはできません。これは誤りである

:ここ

Python version 2.7: XML ElementTree: How to iterate through certain elements of a child element in order to find a match

は、テキストファイルの開閉を無視して、私はこれまでのところ、それが唯一のXMLコードが含まれているコードではAttributeError:「NoneType」オブジェクトには、属性「テキスト」

import xml.etree.ElementTree as et 

# Slice the xml portion of the text file 
myxml = textfile[textfile.index(<"?xml):(textfile.index("</Record")+8)] 
root = fromstring(myxml) 

for admitted in root: 
    date = admitted.find('Admitted').find('Date').text 
    injury = admitted.find('Admitted').find('Injury').text 
    loc = admitted.find('Admitted').find('Location').text 
    print(date) 
    print(injury) 
    print(loc) 

を持っていない私は問題上の任意のアドバイスに感謝されると、事前にあなたの助けに感謝します。

答えて

0

私はXMLコードの解析にminidomを使用します。それは本当に簡単です。以下のコードは、製造元の部分を解析しています。以下の例を参照してください。

import xml.dom.minidom 
import re 

xmlstring=""" 
... and listening to slow jazz <---should not be here 
<?xml version="1.0"?> 
<!DOCTYPE PARTS SYSTEM "parts.dtd"> 
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?> 
<PARTS> 
    <TITLE>Computer Parts</TITLE> 
    <PART> 
     <ITEM>Motherboard</ITEM> 
     <MANUFACTURER>ASUS</MANUFACTURER> 
     <MODEL>P3B-F</MODEL> 
     <COST> 123.00</COST> 
    </PART> 
foo <---should not be here 
    <PART> 
     <ITEM>Video Card</ITEM> 
     <MANUFACTURER>ATI</MANUFACTURER> 
bar <---should not be here 
     <MODEL>All-in-Wonder Pro</MODEL> 
     <COST> 160.00</COST> 
    </PART> 
</PARTS>""" 

#Clean file to use only xml code otherwise minidom wont work 
l=[] 

for line in xmlstring.split('\n'): 
    newxml=re.search(r'<..*>$',line) 
    if newxml: 
     l.append(line.strip()) 
newxml='\n'.join(l) 
#Minidom 
dom = xml.dom.minidom.parseString(newxml) 
Topic=dom.getElementsByTagName('PARTS') 
i = 0 
for node in Topic: 
    alist=node.getElementsByTagName('MANUFACTURER') 
    for a in alist: 
     Title= a.firstChild.data 
     print Title 

#Output would be ASUS and ATI 
0

etreeのdom.findall()を考えてみましょう。 {...}

import lxml.etree as ET 

xmlfile = 'path/to/xml/file.xml' 
dom = ET.parse(xmlfile) 

admitted = dom.findall('{http://www.south.org/}Admitted/*') 

date = []; injury = []; loc = [] 
for i in admitted:  
    if 'Date' in i.tag: date.append(i.text) 
    if 'Injury' in i.tag: injury.append(i.text) 
    if 'Loc' in i.tag: loc.append(i.text) 

print(date) 
print(injury) 
print(loc) 

place = dom.findall('{http://www.south.org/}Place/*/*') 

number = []; wing = []; name = [] 
for i in place:  
    if 'Number' in i.tag: number.append(i.text) 
    if 'Wing' in i.tag: wing.append(i.text) 
    if 'Name' in i.tag: name.append(i.text) 

print(number) 
print(wing) 
print(name) 

出力

# ['2000-11-8t7:24:02'] 
# ['Arm'] 
# ['7'] 
# ['28'] 
# ['East'] 
# ['John Smith'] 
+0

私は初心者だから、ElementTreeのAPIを使用するために探していたとxmlがある:かっこの定義に示すように宣言されていない名前空間を占めるようにしてくださいテキストファイルに埋め込まれています。私の唯一の問題は、その子に正しく移動する根を得ることができないようだと思われる – IronCode

+0

[iter()](https://docs.python.org/2/library/xml.etree.elementtree)を使う必要があります。 html#https://docs.python.org/2/library/xml.etree.elementtree.html#19.7.1.3)、xmlの宣言されていない名前空間を考慮する必要があります。 – Parfait

+0

名前空間!うわー、問題を修正してくれてありがとう:) – IronCode

関連する問題