2016-10-11 2 views
2

나는이는 : 하위 노드의 값이

<category name="28931778o.rjpf"> 
<name>RequestedName</name> 
<root>0</root> 
<online>1</online> 
<description xml:lang="pt-PT">mydescription </description> 
<category-links/> 
<template/> 
<parent name="PTW-0092"/> 
<custom-attributes> 
<custom-attribute name="sortkey" dt:dt="string">RequestedValue</custom-attribute> 
<custom-attribute name="ShortLink" dt:dt="string" xml:lang="pt-PT">/Requested_Url.html</custom-attribute> 
<custom-attribute name="ShortLinkActivate" dt:dt="string" xml:lang="pt-PT">true</custom-attribute> 
... 
</category> 

내가 다시 3 값을 요청 각 카테고리 얻을 필요가 많은 노드 구성 좋아에 큰 파일을 처리 할 필요가 얻을 수 없습니다. Python .27 및 etree를 사용합니다.

for elem in tree.iterfind('{http://www.cc.com/a}category'): 
    requestedName = elem.find('{http://www.cc.com/a}name').text 
    print requestedName 

를 실행할 때 잘 작동

for elem in tree.iterfind('{http://www.cc.com/a}category/{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="sortkey"]'): 
    print elem.text 

을 실행할 때 는 내가 세 가지 값을 retreive 할 경우에도 문제가 온다

잘 작동합니다. 나는 "카테고리"노드를 찾기 위해 노력하고 그 안에

for elem in tree.iterfind('{http://www.cc.com/a}category'): 
    requestedName = elem.find('{http://www.cc.com/a}name').text 
    print requestedName 
    Requestedsortkey = elem.find('./{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="sortkey"]') 
    print Requestedsortkey.text 
    RequestedUrl = elem.find('./{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="ShortLink"]') 
    print RequestedUrl.text 

프로그램 AttributeError 자신의 오류 메시지와 함께 충돌 2 개 요청 값을 찾기 위해 'NoneType'개체가 어떤 속성 '텍스트'

이 없습니다 누가 할 수 도움?

+0

'text' 속성에 액세스하기 전에 객체가'None'인지 확인해야합니다. – dopstar

+0

잘 모르겠지만 한가지 차이점을 보았습니다. 새 버전에서는 경로에'. /'를 사용했지만'name'에는 사용하지 않았습니다. – furas

+0

dopstar에 많은 감사를드립니다. – Chauvinus

답변

0

당신은 바로 Dopstar입니다! 많은 감사

for elem in tree.iterfind('{http://www.cc.com/a}category'): 
    requestedName = elem.find('{http://www.cc.com/a}name').text 
    print requestedName 
    Requestedsortkey = elem.find('{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="sortkey"]') 
    if Requestedsortkey <> None: 
     print Requestedsortkey.text 
    RequestedUrl = elem.find('{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="ShortLink"]') 
    if RequestedUrl <> None : 
     print RequestedUrl.text 
관련 문제