简体   繁体   English

Python用minidom编辑xml

[英]Python edit xml with minidom

Try as I might I just not getting this to work. 尽我所能,试试吧。 I have an xml file: 我有一个xml文件:

: 在此处输入图片说明

I need to change the values of xyz and add a name to the name tag. 我需要更改xyz的值并将名称添加到名称标签。 I just can't seem to figure out how to get there. 我似乎无法弄清楚如何到达那里。

my code: 我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from xml.dom import minidom
xmldoc = minidom.parse('example.xml')


sensorList = xmldoc.getElementsByTagName('sensor')

for sensor in sensorList:
    sensor.firstChild.nodeValue = "test"
    sensor.nextSibling.nodeValue # skip over
    sensor.nextSibling.nodeValue = "1"
    sensor.nextSibling.nodeValue = "2"
    sensor.nextSibling.nodeValue = "3"
    #print sensor.toxml()

xml_file_handle = open('example.xml' , 'wb')
xmldoc.writexml(xml_file_handle)
xml_file_handle.close()

I have tried several variations of sensor.childNodes[0].data or value. 我尝试了sensor.childNodes [0] .data或value的几种变体。 This returns error for no attribute. 这将返回没有属性的错误。 If I use print sensor.toxml() everything is as it should be. 如果我使用print sensor.toxml(),一切都应该是正确的。

when I run this code I do get the test to print , but its inserted right after the "sensor" tag and not the "name" tag. 当我运行这段代码时,我确实可以打印测试,但是它是在“ sensor”标签而不是“ name”标签之后插入的。 Simple syntax issue I know, but I'm just not finding it in the docs. 我知道简单的语法问题,但我只是在文档中找不到它。

Very appreciative for the help. 非常感谢您的帮助。

I could not find a solution with minidom. 我找不到最小的解决方案。

I switched to ElementTree and was able to trip over a solution. 我切换到ElementTree并能够解决一个问题。 What I actually needed to do was read in a .csv file and rewrite the xml with the values from the second file, but that turned out to be trivial compared to getting the xml overwritten. 我实际上需要做的是读入一个.csv文件,并用第二个文件中的值重写xml,但是与覆盖xml相比,这确实是微不足道的。

solution from previous: 以前的解决方案:

#!/usr/bin/python

import sys
import xml.etree.ElementTree as ET
xmlFile = sys.argv[1]

# Xml parsing.
tree = ET.parse(xmlFile)
root = tree.getroot()

#Access the nodes you want by treating root as an array of arrays
# roots children = root[x]
# roots children's children root[x][y]etc

#The array of sensors
sensors = root[1]

for sensor in sensors:
#Access all of sensors children the same way as root.
    sensor[0].text = 'test'
    sensor[3].text = '1'
    sensor[4].text = '2'
    sensor[5].text = '3'

#Open the xmlfile and rewrite it.
xmlFileHandle = open(xmlFile,'wb')
xmlFileHandle.write('<?xml version="1.0" encoding="UTF-8"?> \n' + ET.tostring(root))
xmlFileHandle.close()

Notice the top line of the xml boiler plate needs to be replaced manually. 请注意,需要手动替换xml样板的顶行。 Also you either need to create a custom method to clean prefix name spaces <ns0: tagname > or remove the schema before processing and replace, depending on what your needs are. 另外,您还需要创建一个自定义方法来清理前缀名称空间<ns0:tagname>,或者在处理和替换之前删除架构,具体取决于您的需求。 some people want the prefixes. 有些人想要前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM