티스토리 뷰

Tech/Cording

[Grinder] xml parsing

niu 2013. 11. 25. 02:50

jython에서는 substring과 같은 문자열 처리가 없다.

근데 onetime url 획득 후 데이터 확인해야 한다면?

 

결과물이 xml이라면 아래와 같은 방식으로 xmlData를 추출할수 있다.

 

출처 : http://www.travisglines.com/web-coding/python-xml-parser-tutorial



#import library to do http requests:
import urllib2
 
#import easy to use xml parser called minidom:
from xml.dom.minidom import parseString
#all these imports are standard on most modern python implementations
 
#download the file:
file = urllib2.urlopen('http://www.somedomain.com/somexmlfile.xml')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you downloaded
dom = parseString(data)
#retrieve the first xml tag (data) that the parser finds with name tagName:
xmlTag = dom.getElementsByTagName('tagName')[0].toxml()
#strip off the tag (data  --->   data):
xmlData=xmlTag.replace('','').replace('','')
#print out the xml tag and data in this format: data
print xmlTag
#just print the data
print xmlData
댓글