英文:
Access an element in an XML file and edit them using python
问题
I have a rather sizable xml file that needs to be edited. I am trying to automate the editing process using python. I tried using the 'xml.etree.ElementTree' library in python but it seems like I am missing something. Can someone help me with accessing the elements in the file and how I can edit them?
Here is the xml file I am trying to edit.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:ScheduleDocument xmlns:ecc="xxx" xmlns:ns2="xxx/xxx" xmlns:ns3="xxx/xxx" xmlns:ns4="xxx/xxx" DtdVersion="5" DtdRelease="0">
<ns2:DocumentIdentification v="xxx"/>
<ns2:SenderIdentification v="xxx" codingScheme="A01"/>
<ns2:SenderRole v="A11"/>
<ns2:ReceiverIdentification v="xxx" codingScheme="A01"/>
<ns2:ReceiverRole v="A11"/>
<ns2:DocumentDateTime v="2023-02-23T0"/>
<ns3:ScheduleTimeSeries>
<ns2:CurveType v="xxx"/>
<ns2:Resolution v="xxx"/>
<ns2:ScheduleIdentification v="xxx"/>
<ns2:ScheduleType v="xxx"/>
<ns2:Status v="Confirmed"/>
<ns2:ScheduleTimeInterval v="2023-02-05T00:00+04:00/2023-02-06T03:00+04:00"/>
<ns2:LastModifiedDate>2023-02-23T01:22:13.167+04:00</ns2:LastModifiedDate>
<ns2:IsDraft>false</ns2:IsDraft>
<ns2:Parameter name="xxx" v="2.00000"/>
<ns2:Parameter name="xxx" v="TEST_5_2_23"/>
<ns2:Parameter name="xxx" v="TEST_5_2_23"/>
<ns2:Parameter name="xxx" v="1"/>
<ns2:Parameter name="xxx" v="0"/>
<ns2:Parameter name="xxx" v="xxx"/>
</ns3:ScheduleTimeSeries>
</ns3:ScheduleDocument>
I tried using 'xml.etree.ElementTree' library in python to read and parse the xml file but I did not have success.
Here is the code I built:
from pathlib import Path
import os
import sys
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
PATH = Path(rf'C:\Users\{os.getlogin()}\yyyy\yyyy')
def XMLTree():
tree = ET.parse(os.path.join(PATH, 'xxx', 'xxx.xml'))
root = tree.getroot()
tag = root.tag
attr = root.attrib
for child in root:
print(child.tag, child.attrib)
for i in root.findall('ScheduleTimeSeries'):
print(i) # This prints nothing as I am not able to access ScheduleTimeSeries
print(attr)
for i in root:
print(i.attrib)
for j in root.findall('ns2:CurveType'):
print(j)
if __name__ == '__main__':
print('Starting main')
XMLTree()
print('Main complete')
I am rather new to working with XML files. Can someone tell me what I am missing?
Can someone help me access the elements, say 'Status'. After accessing it, how can I edit it?
英文:
I have a rather sizable xml file that needs to be edited. I am trying to automate the editing process using python. I tried using the 'xml.etree.ElementTree' library in python but it seems like I am missing something. Can someone help me with accessing the elements in the file and how I can edit them?
Here is the xml file I am trying to edit.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:ScheduleDocument xmlns:ecc="xxx" xmlns:ns2="xxx/xxx" xmlns:ns3="xxx/xxx" xmlns:ns4="xxx/xxx" DtdVersion="5" DtdRelease="0">
<ns2:DocumentIdentification v="xxx"/>
<ns2:SenderIdentification v="xxx" codingScheme="A01"/>
<ns2:SenderRole v="A11"/>
<ns2:ReceiverIdentification v="xxx" codingScheme="A01"/>
<ns2:ReceiverRole v="A11"/>
<ns2:DocumentDateTime v="2023-02-23T0"/>
<ns3:ScheduleTimeSeries>
<ns2:CurveType v="xxx"/>
<ns2:Resolution v="xxx"/>
<ns2:ScheduleIdentification v="xxx"/>
<ns2:ScheduleType v="xxx"/>
<ns2:Status v="Confirmed"/>
<ns2:ScheduleTimeInterval v="2023-02-05T00:00+04:00/2023-02-06T03:00+04:00"/>
<ns2:LastModifiedDate>2023-02-23T01:22:13.167+04:00</ns2:LastModifiedDate>
<ns2:IsDraft>false</ns2:IsDraft>
<ns2:Parameter name="xxx" v="2.00000"/>
<ns2:Parameter name="xxx" v="TEST_5_2_23"/>
<ns2:Parameter name="xxx" v="TEST_5_2_23"/>
<ns2:Parameter name="xxx" v="1"/>
<ns2:Parameter name="xxx" v="0"/>
<ns2:Parameter name="xxx" v="xxx"/>
</ns3:ScheduleTimeSeries>
</ns3:ScheduleDocument>
I tried using 'xml.etree.ElementTree' library in python to read and parse the xml file but I did not have success.
Here is the code I built:
from pathlib import Path
import os
import sys
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
PATH = Path(rf'C:\Users\{os.getlogin()}\yyyy\yyyy')
def XMLTree():
tree = ET.parse(os.path.join(PATH, 'xxx', 'xxx.xml'))
root = tree.getroot()
tag = root.tag
attr = root.attrib
for child in root:
print(child.tag, child.attrib)
for i in root.findall('ScheduleTimeSeries'):
print(i) # This prints nothing as I am not able to access ScheduleTimeSeries
print(attr)
for i in root:
print(i.attrib)
for j in root.findall('ns2:CurveType'):
print(j)
if __name__ == '__main__':
print('Starting main')
XMLTree()
print('Main complete')
I am rather new to working with XML files. Can someone tell me what I am missing?
Can someone help me access the elements, say 'Status'. After accessing it, how can I edit it?
答案1
得分: 1
以下是翻译好的代码部分:
root = ET.parse('your_path.xml')
# 查找
elem = root.find('.//{xxx/xxx}CurveType')
print(elem.attrib["v"])
# 编辑
elem.set("v", "new value")
print(elem.attrib["v"])
# 添加
new_elem = ET.SubElement(root.find(".//{xxx/xxx}ScheduleTimeSeries"), "{xxx/xxx}NewElement")
new_elem.text = "added value"
# 写入
with open("ScheduleDocument.xml", "w") as f:
f.write(ET.tostring(root).decode())
英文:
Here a simple example:
root = ET.parse('your_path.xml')
# Finding
elem = root.find('.//{xxx/xxx}CurveType')
print(elem.attrib["v"])
# Editing
elem.set("v", "new value")
print(elem.attrib["v"])
# Adding
new_elem = ET.SubElement(root.find(".//{xxx/xxx}ScheduleTimeSeries"), "{xxx/xxx}NewElement")
new_elem.text = "added value"
# Writing
with open("ScheduleDocument.xml", "w") as f:
f.write(ET.tostring(root).decode())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论