Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

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 »

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 »

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 »

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 222 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 »

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 »

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 »

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