Archive: XML File


XML File
  Is it possible with NSIS to write to a .xml file?


There's no plugin written for creating XML files, but you can create one with the NSIS FileOpen, FileWrite, and FileClose

-Stu


There's no plugin written for creating XML files, but you can create one with the NSIS FileOpen, FileWrite, and FileClose
You can't create plugins with NSIS itself. But you can create a function or macro for the job.

HELP! I am parsing some variables into my xml file, and there are about 300 other lines in the xml file that don't need intervention from the installer. Im using the File Write command, and it is deleting everything else from the file. All 300 lines, and just placing the one line that has the variables in it.

The variables are being parsed correctly, but I am just confused as to why the rest of the file would disappear.

Thanks!


Maybe this is the reason:

FileWrite $0 "blablabla" a


I get this error when I try to do that:

ClearErrors
FileOpen: $INSTDIR\sam2.core.xml as w -> $7
IfErrors ?done:
FileWrite expects 2 parameters, got 3.
Usage: FileWrite $(user_var: handle input) text
Error in script "C:\WINDOWS\Desktop\installer\script.nsi" on line 184 -- aborting creation process
And I cannot open the installer.

EDIT:

This is my code for the filewrite:
ClearErrors
FileOpen $7 $INSTDIR\sam2.core.xml w
IfErrors done
FileWrite $7 '<CONFIG application="SAM" version="2.8.3"><Database><Host>$host</Host><Port>$port</Port><Database>$base</Database><Username>$username</Username><Password>$pass</Password></Database>'
FileClose $7
done:

Originally posted by deguix
You can't create plugins with NSIS itself. But you can create a function or macro for the job.
No deguix, I'm saying he can create an XML file with FileOpen, FileWrite, and FileClose :D

-Stu :p

Change your FileWrite open mode... You're using "w" which means write. You need to use "a" to append.
Also, you need to use FileSeek $7 0 END after FileOpen

Edit: Also you need quotes around your file path on FileOpen.

-Stu


Afrow, what does the FileSeek do in this? I added it anyways. It correctly compiled, and when I tried to view the .xml file, I get this error:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

End tag 'Station' does not match the start tag 'CONFIG'. Error processing resource 'file:///C:/Program Files/WiFOX/SAM2/SAM2.core.xml'. Line 1, Position 232


<CONFIG application="SAM" version="2.8.3"><Database><Host>localhost</Host><Port>3306</Port><Database>SAMDB</Database><Username>rootname</Username><Password>strege</Password></Database>l><ICQ></ICQ><AIM></AIM><TouchAR>-1</TouchAR></Station><Member><MemberID>0</MemberID><Username></Username><Password></Password></Member><Plugins><Readers><e0VDQzIyMEVELTA5ODktNDgwNC1CN0M5LTI1N0NGRTJCMDYxMX0><MustReadInfo>0</MustReadInfo><Name>Standard file reader</Name><Description>Reads files files from standard sources (Harddisk or Network).
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------^

ok. I looked at the xml code page now, and found that all but the last five lines are there. Maybe this will help!


does anybody have any ideas??


No deguix, I'm saying he can create an XML file with FileOpen, FileWrite, and FileClose
I need a wake up call, two posts and I was still not seeing what was really going on! :D I did even put a wrong command syntax there which the correct is:
FileOpen $0 "filename.ext" a
I'm certainly smart ;).

EDIT: I forgot again about the "a" parameter.

Well it says you don't have an end tag for <CONFIG> and it say's you've got an end tag of </Station> there instead...

-Stu


yes. I just checked again, and all of the lines in the xml file are there, it just doenst wanto to read it properly or something.


OK! I think I have figured out the error, but not the solution. The FileWrite command is overwriting the stuff that is alrrady at the beginning of the file. So, It deleted the beginning <EMail> Tag, and everything before that.

Do you know how to get it so NSIS doesn't overwrite the stuff that was previously there. Just to append it to the beginning of the file?


UPDATE: If I use FileOpen $0 "$INSTDIR\sam2.core.xml" w, then It only adds the stuff that is in the script to the beginning, and deletes all the rest, but If I use FileOpen $0 "$INSTDIR\sam2.core.xml" a then it overwrites the stuff at the beginning, but leaves the rest of the doc intact.

Im confused.


Yeah, the "a" mode preserves the content, enables you to read and write to the file at the same time, but when you write, the contents get overwritten with new data.

There is this function called Write to text file line number in the Archive that insert the line in the file, without overwriting some file contents. I tested it and works, but I think that there should be more functions that write to the file that don't overwrite contents :(.

EDIT: It's quite slow though.


by slow what do u mean?


Try it and find out.
It's slow but not that slow so don't worry. A lot of NSIS functions are slow compared to how fast they'd be when written in ie C++.

-Stu


It's slow but not that slow so don't worry. A lot of NSIS functions are slow compared to how fast they'd be when written in ie C++.
That's true. :up:

At the top of this function, I notice:


Exch $0 ;file

Exch
Exch$1 ;line number
Exch 2
Exch$2 ;string to write
Exch 2
>
What do I do here? Should I define these variables earlier in my script? Do I replace the $0 or $1 or $2 with their values?

Thanks!

No you don't need to. The stack (Push, Pop, Exch) does the job of saving your variables, and thus, making possible to use the same variables inside the function. At the end of the function, the old values are returned to those variables, and the new function values go back to the stack.


what I mean is How do I fill in the line number, and file, and string?

Sorry if this seems Newb like.


Just push them in the order said on the Archive page, and customize the parameters from the "Push" commands, like for example:

Push "blablabla"         ;text to insert
Push "13" ;line number
Push "$INSTDIR\file.txt" ;file name
Call WriteToFileLine

okay...Thanks


Thanks all for your help!

I fixed this part of my script!

Arfy