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.
Các hàm tạo cho phần tử và subelement bao gồm ** thêm, chấp nhận các thuộc tính là đối số từ khóa. Điều này cho phép bạn thêm một số lượng thuộc tính tùy ý. Bạn cũng có thể sử dụng sử dụng. Đặt thành thêm các thuộc tính vào một phần tử có sẵn. Làm cách nào để tìm một thẻ cụ thể trong XML trong Python? Sử dụng XML ..: Change the value of xml attribute “category” to “Python” if the title of the book contains “Python“. cây = ElementTree. Parse ("Sample.xml").: 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. root = cây. getroot ().
Sasikiran v
unread,
Ngày 22 tháng 2 năm 2013, 10: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, 10:59:10 PM2/22/132/22/13
đến Pythonpune
Sasikiran v
unread,
Ngày 22 tháng 2 năm 2013, 11:31:58 PM2/22/132/22/13
đến
Tôi đã thử với một số lựa chọn thay thế nhưng nó đang kiểm tra một giá trị thuộc tính và dựa trên giá trị đó nó đang được thay thế.
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, 10:59:10 PM2/22/132/23/13
đến Pythonpune