修改xml的屬性值

2021-10-07 02:50:50 字數 1403 閱讀 7629

# coding=utf-8

import os

import os.path

import xml.dom.minidom

#更改xml檔案中name和folder屬性值

path =

"f:/mx_matting/ssd.pytorch-master/data/vocdevkit/voc2007/annotations"

files = os.listdir(path)

# 得到資料夾下所有檔名稱

for xmlfile in files:

# 遍歷資料夾

ifnot os.path.isdir(xmlfile)

:# 判斷是否是資料夾,不是資料夾才開啟

print

(xmlfile)

# 將獲取的xml檔名送入到dom解析

dom = xml.dom.minidom.parse(os.path.join(path, xmlfile)

)# 輸入xml檔案具體路徑

root = dom.documentelement

# 獲取標籤以及的值

name = root.getelementsbytagname(

'name'

) folder = root.getelementsbytagname(

'folder'

)# 對每個xml檔案的多個同樣的屬性值進行修改。此處將每乙個屬性修改為plane,每乙個屬性修改為voc2007

for i in

range

(len

(name)):

print

(name[i]

.firstchild.data)

name[i]

.firstchild.data =

'person'

print

(name[i]

.firstchild.data)

for i in

range

(len

(folder)):

print

(folder[i]

.firstchild.data)

folder[i]

.firstchild.data =

'voc2007'

print

(folder[i]

.firstchild.data)

# 將屬性儲存至xml檔案中

with

open

(os.path.join(path, xmlfile)

,'w'

)as fh:

dom.writexml(fh)

print

('已寫入'

)

獲取XML中指定屬性的值

1.0 encoding utf 8 orderservice 2 destcode 010 mailno 11111 origincode 021 orderid 778 從廠商那裡返回的這樣的介面,但是正確的是上面標紅的code.所以我就是要先去判斷下 中是否有return tracking n...

Spring用xml注入屬性值

實體 tostring data noargsconstructor allargsconstructor public class inte cepojo實體 data noargsconstructor allargsconstructor tostring public class userp...

修改Feature的屬性表的值

首先要獲取featureclass,利用featureclass獲取iworkspace和iworkspaceedit iworkspace workspace idataset pfeatureclass workspace iworkspaceeditworkspaceedit workspac...