2017-01-25 4 views
0

을 찾을 : <entry key="applications" value="APP_NAME"/>파이썬 etree - 내가 XML 파일 다음 한 정확히 일치

와`: APP_NAME_2을 즉하기 위해 value을 변경

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE TaskDefinition PUBLIC "xxx" "yyy"> 
<TaskDefinition created="time_stamp" formPath="path/sometask.xhtml" id="sample_id" modified="timestamp_b" name="sample_task" resultAction="Delete" subType="subtype_sample_task" type="sample_type"> 
    <Attributes> 
    <Map> 
     <entry key="applications" value="APP_NAME"/> 
     <entry key="aaa" value="true"/> 
     <entry key="bbb" value="true"/> 
     <entry key="ccc" value="true"/> 
     <entry key="ddd" value="true"/> 
     <entry key="eee" value="Disabled"/> 
     <entry key="fff"/> 
     <entry key="ggg"/> 
    </Map> 
    </Attributes> 
    <Description>Description.</Description> 
    <Owner> 
    <Reference class="sample_owner_class" id="sample_owner_id" name="sample__owner_name"/> 
    </Owner> 
    <Parent> 
    <Reference class="sample_parent_class" id="sample_parent_id" name="sample_parent_name"/> 
    </Parent> 
</TaskDefinition> 

내가 검색 할.

나는 내가이로이 값을 추출 할 수 있습니다 알고

import xml.etree.cElementTree as ET 

tree = ET.ElementTree(file='sample.xml') 
root = tree.getroot() 

print(root[0][0][0].tag, root[0][0][0].attrib) 

을하지만,이 경우에 나는 나무에서 THS 항목의 정확한 위치를 알고있다 - 그래서 유연하지 않고, 내가 어떻게 아무 생각이 없다 그것을 바꿔라.

은 또한이 같은 시도 :

for app in root.attrib: 
    if 'applications' in root.attrib: 
     print(app) 

을하지만, 이것은 아무 것도 반환하지 않습니다 이유는, 알아낼 수 없습니다. 예를 거기에 따르고 파이썬 문서에서

:

for rank in root.iter('rank'): 
    new_rank = int(rank.text) + 1 
    rank.text = str(new_rank) 
    rank.set('updated', 'yes')  
tree.write('output.xml') 

하지만 내가 어떻게 내 예를 들어이를 addjust하는 아무 생각이 없습니다. 이 경우 정규 표현식을 사용하고 싶지 않습니다. 도움을 주시면 감사하겠습니다.

답변

1

entry 요소는 XPath으로 찾을 수 있습니다.

import xml.etree.ElementTree as ET 

tree = ET.parse("sample.xml") 

# Find the element that has a 'key' attribute with a value of 'applications' 
entry = tree.find(".//entry[@key='applications']") 

# Change the value of the 'value' attribute 
entry.set("value", "APP_NAME_2") 

tree.write("output.xml") 

결과 (output.xml) :

<TaskDefinition created="time_stamp" formPath="path/sometask.xhtml" id="sample_id" modified="timestamp_b" name="sample_task" resultAction="Delete" subType="subtype_sample_task" type="sample_type"> 
    <Attributes> 
    <Map> 
     <entry key="applications" value="APP_NAME_2" /> 
     <entry key="aaa" value="true"/> 
     <entry key="bbb" value="true"/> 
     <entry key="ccc" value="true"/> 
     <entry key="ddd" value="true"/> 
     <entry key="eee" value="Disabled"/> 
     <entry key="fff"/> 
     <entry key="ggg"/> 
    </Map> 
    </Attributes> 
    <Description>Description.</Description> 
    <Owner> 
    <Reference class="sample_owner_class" id="sample_owner_id" name="sample__owner_name"/> 
    </Owner> 
    <Parent> 
    <Reference class="sample_parent_class" id="sample_parent_id" name="sample_parent_name"/> 
    </Parent> 
</TaskDefinition> 
+0

이 <'내가 인쇄'에 (항목)'때 지금은 내가 얻을 ... 너무 간단 보인다 원하는 excatly입니다 0x00000000015C3E08에 'entry'요소가 있습니다 - '정상 값'을 얻을 수 있습니까? – Fangir

+0

아마도 당신이 원하는 것은'print (entry.tag)'입니까? – mzjn

+0

'entry.text'는 요소에 내용이 없기 때문에 비어 있습니다. – mzjn