minidom

add element with attributes in minidom python

ⅰ亾dé卋堺 提交于 2019-11-30 11:51:06
I want to add a child node with attributes to a specific tag. my xml is <deploy> </deploy> and the output should be <deploy> <script name="xyz" action="stop"/> </deploy> so far my code is: dom = parse("deploy.xml") script = dom.createElement("script") dom.childNodes[0].appendChild(script) dom.writexml(open(weblogicDeployXML, 'w')) script.setAttribute("name", args.script) How can I figure out how to find deploy tag and append child node with attributes ? William A. Romero R. xml.dom.Element.setAttribute xmlFile = minidom.parse( FILE_PATH ) for script in SCRIPTS: newScript = xmlFile

Empty lines while using minidom.toprettyxml

穿精又带淫゛_ 提交于 2019-11-30 08:35:53
I've been using a minidom.toprettyxml for prettify my xml file. When I'm creating XML file and using this method, all works grate, but if I use it after I've modified the xml file (for examp I've added an additional nodes) and then I'm writing it back to XML, I'm getting empty lines, each time I'm updating it, I'm getting more and more empty lines... my code : file.write(prettify(xmlRoot)) def prettify(elem): rough_string = xml.tostring(elem, 'utf-8') //xml as ElementTree reparsed = mini.parseString(rough_string) //mini as minidom return reparsed.toprettyxml(indent=" ") and the result : <?xml

Empty lines while using minidom.toprettyxml

末鹿安然 提交于 2019-11-29 11:31:48
问题 I've been using a minidom.toprettyxml for prettify my xml file. When I'm creating XML file and using this method, all works grate, but if I use it after I've modified the xml file (for examp I've added an additional nodes) and then I'm writing it back to XML, I'm getting empty lines, each time I'm updating it, I'm getting more and more empty lines... my code : file.write(prettify(xmlRoot)) def prettify(elem): rough_string = xml.tostring(elem, 'utf-8') //xml as ElementTree reparsed = mini

Edit XML file text based on path

谁说胖子不能爱 提交于 2019-11-29 09:46:16
I have an XML file (e.g. jerry.xml) which contains some data as given below. <data> <country name="Peru"> <rank updated="yes">2</rank> <language>english</language> <currency>1.21$/kg</currency> <gdppc month="06">141100</gdppc> <gdpnp month="10">2.304e+0150</gdpnp> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <language>english</language> <currency>4.1$/kg</currency> <gdppc month="05">59900</gdppc> <gdpnp month="08">1.9e-015</gdpnp> <neighbor name="Malaysia" direction="N"/> </country> I

XML Parsing with Python and minidom

纵然是瞬间 提交于 2019-11-28 07:34:12
I'm using Python (minidom) to parse an XML file that prints a hierarchical structure that looks something like this (indentation is used here to show the significant hierarchical relationship): My Document Overview Basic Features About This Software Platforms Supported Instead, the program iterates multiple times over the nodes and produces the following, printing duplicate nodes. (Looking at the node list at each iteration, it's obvious why it does this but I can't seem to find a way to get the node list I'm looking for.) My Document Overview Basic Features About This Software Platforms

Reading XML using Python minidom and iterating over each node

空扰寡人 提交于 2019-11-28 05:52:10
I have an XML structure that looks like the following, but on a much larger scale: <root> <conference name='1'> <author> Bob </author> <author> Nigel </author> </conference> <conference name='2'> <author> Alice </author> <author> Mary </author> </conference> </root> For this, I used the following code: dom = parse(filepath) conference=dom.getElementsByTagName('conference') for node in conference: conf_name=node.getAttribute('name') print conf_name alist=node.getElementsByTagName('author') for a in alist: authortext= a.nodeValue print authortext However, the authortext that is printed out is

Python XML: write " instead of &quot

强颜欢笑 提交于 2019-11-28 04:16:31
问题 I am using Python's xml minidom and all works well except that in text sequences it writes out &quot escape characters instead of " . This of course makes sense if a quote appears in a tag, but it bugs me in the text. How do I change this? 回答1: looking at the source (Python 3.2 if it matters), this is hardcoded in the _write_data() function. you would need to modify the writexml() method of TextNode - either by subclassing it or simply editing it - so that it didn't call that method, but

Edit XML file text based on path

感情迁移 提交于 2019-11-28 03:06:40
问题 I have an XML file (e.g. jerry.xml) which contains some data as given below. <data> <country name="Peru"> <rank updated="yes">2</rank> <language>english</language> <currency>1.21$/kg</currency> <gdppc month="06">141100</gdppc> <gdpnp month="10">2.304e+0150</gdpnp> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <language>english</language> <currency>4.1$/kg</currency> <gdppc month="05"

Preserve order of attributes when modifying with minidom

不想你离开。 提交于 2019-11-27 22:19:55
Is there a way I can preserve the original order of attributes when processing XML with minidom? Say I have: <color red="255" green="255" blue="233" /> when I modify this with minidom the attributes are rearranged alphabetically blue, green, and red. I'd like to preserve the original order. I am processing the file by looping through the elements returned by elements = doc.getElementsByTagName('color') and then I do assignments like this e.attributes["red"].value = "233" . Is there a way I can preserve the original order of attributes when processing XML with minidom? With minidom no, the

Get Element value with minidom with Python

半城伤御伤魂 提交于 2019-11-27 10:32:51
I am creating a GUI frontend for the Eve Online API in Python. I have successfully pulled the XML data from their server. I am trying to grab the value from a node called "name": from xml.dom.minidom import parse dom = parse("C:\\eve.xml") name = dom.getElementsByTagName('name') print name This seems to find the node, but the output is below: [<DOM Element: name at 0x11e6d28>] How could I get it to print the value of the node? eduffy It should just be name[0].firstChild.nodeValue Probably something like this if it's the text part you want... from xml.dom.minidom import parse dom = parse("C:\