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.
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
Write to XML File
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.
- Pako
- Plugin Developer
- Posts: 2294
- Joined: Sat Nov 11, 2006 1:31 pm
- Location: Czech Republic
- Contact:
Re: Write to XML File
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
Then I can better advise.
Pako
Re: Write to XML File
Attached is a sample xml file.
I want to update this file to add the following lines before the last line ("</PayloadsRoot>"):
The value "test3" is going to be from an Eventghost variable {eg.result}.
Thank you Pako for replying.
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 221 times
- Pako
- Plugin Developer
- Posts: 2294
- Joined: Sat Nov 11, 2006 1:31 pm
- Location: Czech Republic
- Contact:
Re: Write to XML File
Here is a script:Pako
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()Re: Write to XML File
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).
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).
- Pako
- Plugin Developer
- Posts: 2294
- Joined: Sat Nov 11, 2006 1:31 pm
- Location: Czech Republic
- Contact:
Re: Write to XML File
Usually it is not necessary. To view the contents of a xml file is much better to use the XML Notepad.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).
But if you really need it, take a look here.
Pako