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

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

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.

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <ns3:ScheduleDocument xmlns:ecc="xxx" xmlns:ns2="xxx/xxx" xmlns:ns3="xxx/xxx" xmlns:ns4="xxx/xxx" DtdVersion="5" DtdRelease="0">
  3. <ns2:DocumentIdentification v="xxx"/>
  4. <ns2:SenderIdentification v="xxx" codingScheme="A01"/>
  5. <ns2:SenderRole v="A11"/>
  6. <ns2:ReceiverIdentification v="xxx" codingScheme="A01"/>
  7. <ns2:ReceiverRole v="A11"/>
  8. <ns2:DocumentDateTime v="2023-02-23T0"/>
  9. <ns3:ScheduleTimeSeries>
  10. <ns2:CurveType v="xxx"/>
  11. <ns2:Resolution v="xxx"/>
  12. <ns2:ScheduleIdentification v="xxx"/>
  13. <ns2:ScheduleType v="xxx"/>
  14. <ns2:Status v="Confirmed"/>
  15. <ns2:ScheduleTimeInterval v="2023-02-05T00:00+04:00/2023-02-06T03:00+04:00"/>
  16. <ns2:LastModifiedDate>2023-02-23T01:22:13.167+04:00</ns2:LastModifiedDate>
  17. <ns2:IsDraft>false</ns2:IsDraft>
  18. <ns2:Parameter name="xxx" v="2.00000"/>
  19. <ns2:Parameter name="xxx" v="TEST_5_2_23"/>
  20. <ns2:Parameter name="xxx" v="TEST_5_2_23"/>
  21. <ns2:Parameter name="xxx" v="1"/>
  22. <ns2:Parameter name="xxx" v="0"/>
  23. <ns2:Parameter name="xxx" v="xxx"/>
  24. </ns3:ScheduleTimeSeries>
  25. </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:

  1. from pathlib import Path
  2. import os
  3. import sys
  4. from bs4 import BeautifulSoup
  5. import xml.etree.ElementTree as ET
  6. PATH = Path(rf'C:\Users\{os.getlogin()}\yyyy\yyyy')
  7. def XMLTree():
  8. tree = ET.parse(os.path.join(PATH, 'xxx', 'xxx.xml'))
  9. root = tree.getroot()
  10. tag = root.tag
  11. attr = root.attrib
  12. for child in root:
  13. print(child.tag, child.attrib)
  14. for i in root.findall('ScheduleTimeSeries'):
  15. print(i) # This prints nothing as I am not able to access ScheduleTimeSeries
  16. print(attr)
  17. for i in root:
  18. print(i.attrib)
  19. for j in root.findall('ns2:CurveType'):
  20. print(j)
  21. if __name__ == '__main__':
  22. print('Starting main')
  23. XMLTree()
  24. 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.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
  2. &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;
  3. &lt;ns2:DocumentIdentification v=&quot;xxx&quot;/&gt;
  4. &lt;ns2:SenderIdentification v=&quot;xxx&quot; codingScheme=&quot;A01&quot;/&gt;
  5. &lt;ns2:SenderRole v=&quot;A11&quot;/&gt;
  6. &lt;ns2:ReceiverIdentification v=&quot;xxx&quot; codingScheme=&quot;A01&quot;/&gt;
  7. &lt;ns2:ReceiverRole v=&quot;A11&quot;/&gt;
  8. &lt;ns2:DocumentDateTime v=&quot;2023-02-23T0&quot;/&gt;
  9. &lt;ns3:ScheduleTimeSeries&gt;
  10. &lt;ns2:CurveType v=&quot;xxx&quot;/&gt;
  11. &lt;ns2:Resolution v=&quot;xxx&quot;/&gt;
  12. &lt;ns2:ScheduleIdentification v=&quot;xxx&quot;/&gt;
  13. &lt;ns2:ScheduleType v=&quot;xxx&quot;/&gt;
  14. &lt;ns2:Status v=&quot;Confirmed&quot;/&gt;
  15. &lt;ns2:ScheduleTimeInterval v=&quot;2023-02-05T00:00+04:00/2023-02-06T03:00+04:00&quot;/&gt;
  16. &lt;ns2:LastModifiedDate&gt;2023-02-23T01:22:13.167+04:00&lt;/ns2:LastModifiedDate&gt;
  17. &lt;ns2:IsDraft&gt;false&lt;/ns2:IsDraft&gt;
  18. &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;2.00000&quot;/&gt;
  19. &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;TEST_5_2_23&quot;/&gt;
  20. &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;TEST_5_2_23&quot;/&gt;
  21. &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;1&quot;/&gt;
  22. &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;0&quot;/&gt;
  23. &lt;ns2:Parameter name=&quot;xxx&quot; v=&quot;xxx&quot;/&gt;
  24. &lt;/ns3:ScheduleTimeSeries&gt;
  25. &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:

  1. from pathlib import Path
  2. import os
  3. import sys
  4. from bs4 import BeautifulSoup
  5. import xml.etree.ElementTree as ET
  6. PATH = Path(rf&#39;C:\Users\{os.getlogin()}\yyyy\yyyy&#39;)
  7. def XMLTree():
  8. tree = ET.parse(os.path.join(PATH, &#39;xxx&#39;, &#39;xxx.xml&#39;))
  9. root = tree.getroot()
  10. tag = root.tag
  11. attr = root.attrib
  12. for child in root:
  13. print(child.tag, child.attrib)
  14. for i in root.findall(&#39;ScheduleTimeSeries&#39;):
  15. print(i) # This prints nothing as I am not able to access ScheduleTimeSeries
  16. print(attr)
  17. for i in root:
  18. print(i.attrib)
  19. for j in root.findall(&#39;ns2:CurveType&#39;):
  20. print(j)
  21. if __name__ == &#39;__main__&#39;:
  22. print(&#39;Starting main&#39;)
  23. XMLTree()
  24. 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

以下是翻译好的代码部分:

  1. root = ET.parse('your_path.xml')
  2. # 查找
  3. elem = root.find('.//{xxx/xxx}CurveType')
  4. print(elem.attrib["v"])
  5. # 编辑
  6. elem.set("v", "new value")
  7. print(elem.attrib["v"])
  8. # 添加
  9. new_elem = ET.SubElement(root.find(".//{xxx/xxx}ScheduleTimeSeries"), "{xxx/xxx}NewElement")
  10. new_elem.text = "added value"
  11. # 写入
  12. with open("ScheduleDocument.xml", "w") as f:
  13. f.write(ET.tostring(root).decode())
英文:

Here a simple example:

  1. root = ET.parse(&#39;your_path.xml&#39;)
  2. # Finding
  3. elem = root.find(&#39;.//{xxx/xxx}CurveType&#39;)
  4. print(elem.attrib[&quot;v&quot;])
  5. # Editing
  6. elem.set(&quot;v&quot;, &quot;new value&quot;)
  7. print(elem.attrib[&quot;v&quot;])
  8. # Adding
  9. new_elem = ET.SubElement(root.find(&quot;.//{xxx/xxx}ScheduleTimeSeries&quot;), &quot;{xxx/xxx}NewElement&quot;)
  10. new_elem.text = &quot;added value&quot;
  11. # Writing
  12. with open(&quot;ScheduleDocument.xml&quot;, &quot;w&quot;) as f:
  13. 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:

确定