-1
여기서 xml 파일의 예는 다음과 같습니다. houseNum은 연속적이지 않습니다.lxml을 사용하여 XML 파일의 element.text를 변경하는 방법
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfHouse>
<XmlForm>
<houseNum>0</houseNum>
<plan1>
<coord>
<X> 1.2 </X>
<Y> 2.1 </Y>
<Z> 3.0 </Z>
</coord>
<color>
<R> 255 </R>
<G> 0 </G>
<B> 0 </B>
</color>
</plan1>
<plan2>
<coord>
<X> 21.2 </X>
<Y> 22.1 </Y>
<Z> 31.0 </Z>
</coord>
<color>
<R> 255 </R>
<G> 0 </G>
<B> 0 </B>
</color>
</plan2>
</XmlForm>
<XmlForm>
<houseNum>3</houseNum>
<plan1>
<coord>
<X> 1.2 </X>
<Y> 2.1 </Y>
<Z> 3.0 </Z>
</coord>
<color>
<R> 255 </R>
<G> 0 </G>
<B> 0 </B>
</color>
</plan1>
<plan2>
<coord>
<X> 21.2 </X>
<Y> 22.1 </Y>
<Z> 31.0 </Z>
</coord>
<color>
<R> 255 </R>
<G> 0 </G>
<B> 0 </B>
</color>
</plan2>
</XmlForm>
<XmlForm>
<houseNum>5</houseNum>
<plan1>
<coord>
<X> 11.2 </X>
<Y> 12.1 </Y>
<Z> 13.0 </Z>
</coord>
<color>
<R> 255 </R>
<G> 255 </G>
<B> 0 </B>
</color>
</plan1>
<plan2>
<coord>
<X> 211.2 </X>
<Y> 212.1 </Y>
<Z> 311.0 </Z>
</coord>
<color>
<R> 255 </R>
<G> 0 </G>
<B> 255 </B>
</color>
</plan2>
</XmlForm>
</ArrayOfHouse>
내가 대신 0, 3의 0,1,3 할는 .text을 수정하려면, 5.Here는 잘못된 결과를 생성 LXML를 사용하여 내 스크립트입니다.
def Rename_Elem_In_Xml_File(xml_file, index_start, index_end):
# Parse from the xml string file
tree = etree.parse(xml_file)
#Rename the elements
root = tree.getroot()
elementlist = [e for e in root.iter('houseNum')]
for index_element in range(1, len(elementlist)):
try:
if(index_element>index_start):
elementlist[index_element].text=index_element
print(elementlist[index_element].text)
except:
print(' except ')
print(elementlist[index_element].text)
file = etree.tostring(root).decode("utf-8")
print(file)
필자는 요소를 반복하고 텍스트를 변경하려고합니다. 이 스크립트는 텍스트와 태그의 시작 부분을 삭제합니다.