Hướng dẫn change attribute value xml python - thay đổi giá trị thuộc tính xml python

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?

<?xml version='1.0' encoding='UTF-8'?>
<bookstore>  
  <book category="Programming">  
    <title lang="en">Python Tutorial</title>  
    <level>Beginner</level>  
    <year>2018</year>  
    <price>50.00</price>  
  </book>  
  <book category="Tutorial">  
    <title lang="en">Python Basics</title>  
    <level>Advanced</level>  
    <year>2019</year>  
    <price>45.00</price>  
  </book>    
</bookstore>

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.

import xml.etree.ElementTree as ET
filename = "example.xml"
xmlTree = ET.parse(filename)
rootElement = xmlTree.getroot()
#Iterate Through All Books
for element in rootElement.findall("book"):
    #Check if title contains the word Python
    if 'Python' in element.find('title').text :
        #Change xml attribute value
        element.set('category','Python')
#Write the modified file.        
xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)    

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

Làm thế nào để cập nhật thuộc tính XML trong Python?

Sử dụng Cú pháp Update.Text = "new_text" để thêm văn bản "new_text" vào bản cập nhật SubLement.Sử dụng Cập nhật cú pháp.Attrible ["key"] = "giá trị" để thêm thuộc tính với "khóa" và "giá trị" vào bản cập nhật SubLement. text = "new_text" to add the text "new_text" to a subelement update . Use syntax update. attrib["key"] = "value" to add the attribute with "key" and "value" to a subelement update .

Làm thế nào để thêm thuộc tính trong tệp XML bằng Python?

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.. This allows you to add an arbitrary number of attributes. You can also use use . set to add attributes to a pre-existing element.

Làm cách nào để tìm một thẻ cụ thể trong XML trong Python?

Sử dụng XML ...
cây = ElementTree.Parse ("Sample.xml").
root = cây.getroot ().
Chó = Root.Findall ("Dog").
cho chó ở chó:.
In (chó. Văn bản).

XML Etree ElementTree là gì?

Mô -đun xml.etree.elementtree thực hiện API đơn giản và hiệu quả để phân tích và tạo dữ liệu XML.Thay đổi trong phiên bản 3.3: Mô -đun này sẽ sử dụng triển khai nhanh bất cứ khi nào có sẵn.implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.