Python xml 분석 기록
1600 단어 Python
xml. etree. ElementTree 만 사 용 했 기 때문에 xml. etree. ElementTree 와 관련 된 것 을 간단하게 기록 합 니 다.
기본 읽 기와 쓰기
참고:http://blog.csdn.net/wklken/article/details/7603071
xmlTree = ElementTree.parse('a.xml') #
xmlNodeList = xmlTree.getiterator('Item') #
for node in xmlNodeList:
print 'node.tag:%s' % node.tag #
print 'node.text:%s' % node.text #
if node.attrib.has_key('attr_name'): # attribute
print 'node.attrib[%s]:%s' % ('attr_name',node.attrib['attr_name']))
keyPath = node.attrib['attr_name']
xml 에 namespace 가 들 어 있 는 구덩이
xml 파일 에 namespace 가 있 으 면 Element Tree 는 namespace 의 이름 을 수정 합 니 다. 보통 ns0 같은 것 으로 바 꿉 니 다. 변경 되 기 를 원 하지 않 으 면 이렇게 합 니 다.
XML_NS_NAME = 'my_ns'
XML_NS_VALUE = 'http://xxx'
ElementTree.register_namespace(XML_NS_NAME, XML_NS_VALUE) # parse
namespace 가 있 을 때 namespace 를 바 꿔 야 하 는 노드 를 찾 습 니 다. 예 를 들 어 하나의 attr 는: my 입 니 다.ns: name, 그러면 Element Tree 내부 에서 {로 해 석 됩 니 다.http://xxx} name, 그래서 찾 을 때 도 {를 사용 해 야 합 니 다.http://xxx} name 을 찾 을 수 있 습 니 다.
변환 함수 추가:
#xml namespace ,
def ParseNameSpace(src, nsName, nsValue):
if src.find(nsName) != -1:
dst = src.replace('%s:' % nsName, '{%s}' % nsValue)
print 'ns src:%s dst:%s' % (src, dst)
return dst
return src
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.