Here is the code:
from xml.dom.minidom import Document
doc = Document()
root = doc.createElement('root')
doc.appendChild(root)
for i in range(1,3):
main = doc.createElement('item class:=memory')
root.appendChild(main)
for j in range(1,3):
text = doc.createTextNode('DIMM Size'+str(j))
main.appendChild(text)
print (doc.toprettyxml(indent='\t'))
Here is the output:
<?xml version="1.0" ?>
<root>
<item class:=memory>
DIMM Size1
DIMM Size2
</item class:=memory>
<item class:=memory>
DIMM Size1
DIMM Size2
</item class:=memory>
</root>
I am trying to generate the file with following code. Is there a way to generate the following output:
<root>
<item class:=memory>
<p> DIMM Size1 </p>
<p>DIMM Size2 </p>
</item>
<item class:=memory>
<p>DIMM Size1</p>
<p>DIMM Size2</p>
</item>
</root>
You need two quick changes
pelement e.g.doc.createElement('p')main.attributes['class']='memory'so your code should look like this
A long term change would be to use ElementTree which has more intuitive interface and is easy to use, more so while reading xml e.g. your example in element tree