Tôi cần thay đổi giá trị của một thuộc tính có tên approved-by trong tệp XML từ 'Không' thành 'Có'. Đây là tệp XML của tôi:
<?xml version="1.0" encoding="UTF-8" ?> <!--Arbortext, Inc., 1988-2008, v.4002--> <!DOCTYPE doc PUBLIC "-//MYCOMPANY//DTD XSEIF 1/FAD 110 05 R5//EN" "XSEIF_R5.dtd"> <doc version="XSEIF R5" xmlns="urn:x-mycompany:r2:reg-doc:1551-fad.110.05:en:*"> <meta-data> <?Pub Dtl?> <confidentiality class="mycompany-internal" /> <doc-name>INSTRUCTIONS</doc-name> <doc-id> <doc-no type="registration">1/1531-CRA 119 1364/2</doc-no> <language code="en" /> <rev>PA1</rev> <date> <y>2013</y> <m>03</m> <d>12</d> </date> </doc-id> <company-id> <business-unit></business-unit> <company-name></company-name> <company-symbol logotype="X"></company-symbol> </company-id> <title>SIM Software Installation Guide</title> <drafted-by> <person> <name>Shahul Hameed</name> <signature>epeeham</signature> </person> </drafted-by> <approved-by approved="no"> <person> <name>AB</name> <signature>errrrrn</signature> </approved-by>Tôi đã thử theo hai cách, và thất bại trong cả hai. Cách đầu tiên của tôi là
import xml.etree.ElementTree as ET from xml.etree.ElementTree import Element root = ET.parse('Path/1_1531-CRA 119 1364_2.xml') sh = root.find('approved-by') sh.set('approved', 'yes') print etree.tostring(root)Theo cách này, tôi đã nhận được một thông báo lỗi cho biết AttributeError: 'NoneType' object has no attribute 'set'.
Vì vậy, tôi đã thử một cách khác.
import xml.etree.ElementTree as ET from xml.etree.ElementTree import Element root = ET.parse('C:/Path/1_1531-CRA 119 1364_2.xml') elem = Element("approved-by") elem.attrib["approved"] = "yes"Tôi cũng không gặp bất kỳ lỗi nào, nó cũng không đặt thuộc tính. Tôi bối rối, và không thể tìm thấy có gì sai với tập lệnh này.
Làm thế nào để sửa đổi tệp XML trong Python? Yếu tố. Đặt ('attrname', 'value') - sửa đổi các thuộc tính phần tử .. Yếu tố. Subelement (cha mẹ, new_childtag) -tạo ra một thẻ con mới dưới cha mẹ ..: Change the value of xml attribute “category” to “Python” if the title of the book contains “Python“. Giải pháp: Trước tiên, chúng tôi phân tích tệp XML và thực hiện chuỗi chứa hoạt động để kiểm tra xem tiêu đề của cuốn sách có chứa Py Python hay không. Nếu chuỗi chứa từ khóa, chúng tôi sử dụng phương thức set () để thay đổi giá trị thuộc tính.: First we parse the xml file and perform the
string contains operation to check if the title of the book contains “Python“. If the string contains the keyword, we use the set() method to change the attribute value. Chạy mã ví dụ trên và bạn sẽ thấy tệp XML gốc đã được sửa đổi. Giá trị danh mục của nó bây giờ là Python, cho những cuốn sách có tiêu đề phù hợp.
Sasikiran v
unread,
Ngày 22 tháng 2 năm 2013, 7:40:08 PM2/22/132/22/13
đến
Hi,
Tôi muốn cập nhật thuộc tính XML
Ví dụ như ví dụ.
& nbsp; & nbsp; & nbsp; & nbsp; /usr/bin/kvm & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [b] & nbsp; & nbsp; [/b] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
/usr/bin/kvm
[b] [/b]
Đây là tệp xml mẫu
Bạn có thể vui lòng giúp triển khai tập lệnh Python để cập nhật tệp = 'hello.txt "(dòng in đậm)
Lưu ý: Có hai thẻ nguồn nhưng tôi muốn thẻ nguồn chứa tệp dưới dạng thuộc tính được cập nhật bằng file = 'hello.txt'
Cảm ơn trước
Sasikiran
Dhananjay Nene
unread,
Ngày 22 tháng 2 năm 2013, 7:59:10 PM2/22/132/22/13
đến Pythonpune
Sasikiran v
unread,
Ngày 22 tháng 2 năm 2013, 7:40:08 PM2/22/132/22/13
đến
Tôi muốn cập nhật thuộc tính XML
Ví dụ như ví dụ.
#!/usr/bin/python
& nbsp; & nbsp; & nbsp; & nbsp; /usr/bin/kvm & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [b] & nbsp; & nbsp; [/b] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
Đây là tệp xml mẫu
Bạn có thể vui lòng giúp triển khai tập lệnh Python để cập nhật tệp = 'hello.txt "(dòng in đậm)
"""
print
"********************"
print text
print "*******************"
Lưu ý: Có hai thẻ nguồn nhưng tôi muốn thẻ nguồn chứa tệp dưới dạng thuộc tính được cập nhật bằng file = 'hello.txt'
for node in elem.find('phoneNumbers'):
print node.attrib['topic']
# Create sub elements
if node.attrib['topic']=="sys/phoneNumber/1":
tag = SubElement(node,'TagName')
tag.attrib['attr'] = 'AttribValue'
Cảm ơn trước
Sasikiran
if node.attrib['topic']=="sys/phoneNumber/1":
Dhananjay Nene
Dhananjay Nene
unread,
Ngày 22 tháng 2 năm 2013, 7:59:10 PM2/22/132/22/13
đến Pythonpune