Write to XML File

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
kalia
Experienced User
Posts: 109
Joined: Wed Aug 12, 2009 1:10 am

Write to XML File

Post by kalia » Tue Apr 05, 2011 6:18 pm

I am trying to write a script that will update an existing xml file with a new element (derived from eg.result). I was looking at the File Operations plugin but concluded that this would not work (have to add the entry before the end tag of file). Can anyone help with this. Thanks.

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Write to XML File

Post by Pako » Tue Apr 19, 2011 6:35 am

Please attach a sample XML file and also how to be an additional element incorporated (into which the node).
Then I can better advise.

Pako

kalia
Experienced User
Posts: 109
Joined: Wed Aug 12, 2009 1:10 am

Re: Write to XML File

Post by kalia » Thu Apr 21, 2011 4:22 am

Attached is a sample xml file.

I want to update this file to add the following lines before the last line ("</PayloadsRoot>"):

Code: Select all

<payload>
	<value>test3</value>
	<phrase>test3</phrase>
</payload>



The value "test3" is going to be from an Eventghost variable {eg.result}.

Thank you Pako for replying.
Attachments
sample.xml
Sample XML File
(221 Bytes) Downloaded 244 times

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Write to XML File

Post by Pako » Thu Apr 21, 2011 8:09 am

Here is a script:

Code: Select all

filePath = r"D:\Data\sample.xml"

from xml.dom import minidom as miniDom
from codecs import lookup

xmldoc = miniDom.parse(filePath)
document = xmldoc.getElementsByTagName('PayloadsRoot')

payloadNode = xmldoc.createElement(u'payload')


for item in (u'value', u'phrase'):
    itemNode = xmldoc.createElement(item)
    itemText = xmldoc.createTextNode(unicode(eg.result))          
    itemNode.appendChild(itemText)
    payloadNode.appendChild(itemNode)

document[0].appendChild(payloadNode)


f = file(filePath, "wb")
writer = lookup('utf-8')[3](f)
xmldoc.writexml(writer, encoding = 'utf-8')
f.close()
Pako

kalia
Experienced User
Posts: 109
Joined: Wed Aug 12, 2009 1:10 am

Re: Write to XML File

Post by kalia » Fri Apr 22, 2011 6:17 am

Thank you Pako for your help. I would not have figured that out on my own (my Google search was way off).

The script worked fine. I am going to try to find out if the output can be formatted properly (the script writes new entry unformatted without tabs and line feeds).

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Write to XML File

Post by Pako » Fri Apr 22, 2011 10:59 am

kalia wrote:I am going to try to find out if the output can be formatted properly (the script writes new entry unformatted without tabs and line feeds).
Usually it is not necessary. To view the contents of a xml file is much better to use the XML Notepad.
But if you really need it, take a look here.

Pako

Post Reply