访问XML文件中的元素并使用Python进行编辑。

huangapple go评论64阅读模式
英文:

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;ns3:ScheduleDocument xmlns:ecc=&quot;xxx&quot; xmlns:ns2=&quot;xxx/xxx&quot; xmlns:ns3=&quot;xxx/xxx&quot; xmlns:ns4=&quot;xxx/xxx&quot; DtdVersion=&quot;5&quot; DtdRelease=&quot;0&quot;&gt;
    &lt;ns2:DocumentIdentification v=&quot;xxx&quot;/&gt;
    &lt;ns2:SenderIdentification v=&quot;xxx&quot; codingScheme=&quot;A01&quot;/&gt;
    &lt;ns2:SenderRole v=&quot;A11&quot;/&gt;
    &lt;ns2:ReceiverIdentification v=&quot;xxx&quot; codingScheme=&quot;A01&quot;/&gt;
    &lt;ns2:ReceiverRole v=&quot;A11&quot;/&gt;
    &lt;ns2:DocumentDateTime v=&quot;2023-02-23T0&quot;/&gt;
    &lt;ns3:ScheduleTimeSeries&gt;
        &lt;ns2:CurveType v=&quot;xxx&quot;/&gt;
        &lt;ns2:Resolution v=&quot;xxx&quot;/&gt;
        &lt;ns2:ScheduleIdentification v=&quot;xxx&quot;/&gt;
        &lt;ns2:ScheduleType v=&quot;xxx&quot;/&gt;
        &lt;ns2:Status v=&quot;Confirmed&quot;/&gt;
        &lt;ns2:ScheduleTimeInterval v=&quot;2023-02-05T00:00+04:00/2023-02-06T03:00+04:00&quot;/&gt;
        &lt;ns2:LastModifiedDate&gt;2023-02-23T01:22:13.167+04:00&lt;/ns2:LastModifiedDate&gt;
        &lt;ns2:IsDraft&gt;false&lt;/ns2:IsDraft&gt;
        &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;2.00000&quot;/&gt;
        &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;TEST_5_2_23&quot;/&gt;
        &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;TEST_5_2_23&quot;/&gt;
        &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;1&quot;/&gt;
        &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;0&quot;/&gt;
        &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;xxx&quot;/&gt;
    &lt;/ns3:ScheduleTimeSeries&gt;
&lt;/ns3:ScheduleDocument&gt;

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&#39;C:\Users\{os.getlogin()}\yyyy\yyyy&#39;)


def XMLTree():
    tree = ET.parse(os.path.join(PATH, &#39;xxx&#39;, &#39;xxx.xml&#39;))
    root = tree.getroot()
    tag = root.tag
    attr = root.attrib
    for child in root:
        print(child.tag, child.attrib)
    for i in root.findall(&#39;ScheduleTimeSeries&#39;):
        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(&#39;ns2:CurveType&#39;):
        print(j)


if __name__ == &#39;__main__&#39;:
    print(&#39;Starting main&#39;)
    XMLTree()
    print(&#39;Main complete&#39;)

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(&#39;your_path.xml&#39;)

# Finding
elem = root.find(&#39;.//{xxx/xxx}CurveType&#39;)
print(elem.attrib[&quot;v&quot;])

# Editing
elem.set(&quot;v&quot;, &quot;new value&quot;)
print(elem.attrib[&quot;v&quot;])

# Adding
new_elem = ET.SubElement(root.find(&quot;.//{xxx/xxx}ScheduleTimeSeries&quot;), &quot;{xxx/xxx}NewElement&quot;)
new_elem.text = &quot;added value&quot;

# Writing
with open(&quot;ScheduleDocument.xml&quot;, &quot;w&quot;) as f:
    f.write(ET.tostring(root).decode())

huangapple
  • 本文由 发表于 2023年2月24日 09:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定