Announcement

Collapse
No announcement yet.

Add entry into XML file after specified code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add entry into XML file after specified code

    Hello,

    I'm new to installers thing, I'm currently checking if what I need to do could be done with Setup Factory (I'm using trial version now).

    So, I've got an XML file

    <DATA>
    <REGION>
    <SPEC>K</SPEC>
    <CLASS>
    [...]
    </CLASS>
    </REGION>
    <REGION>
    <SPEC>J</SPEC>
    <CLASS>
    [...]
    </CLASS>
    </REGION>
    </DATA>

    I need my installer to open the file, look for <SPEC>J</SPEC> and just after that insert new <CLASS> entry.

    I thought to add this entry to Ready to install -> On Next action like this:

    XML.Load("%AppFolder%//XMLlocation//the.xml");

    <SPEC>J</SPEC> = XML.GetElementXML("%AppFolder%//XMLlocation//the.xml");

    XML.InsertXML("%AppFolder%//XMLlocation//the.xml", "<CLASS><NAME>TEST</NAME></CLASS>", XML.INSERT_AFTER);

    XML.Save("%AppFolder%//XMLlocation//the.xml");

    It doesn't seem to be working. This is my first attempt to create installer, any help on this would be much appreciated.

    Tom

  • #2
    Hello, please attach the real XML file, and a precise description of how you need to manipulate the file, and I will try to assist.

    Ulrich

    Comment


    • #3
      Hello,

      Let me specify.

      Installer should look for file Default.xml in location:

      HKEY_CURRENT_USER\Software\Lockheed Martin\Prepar3D v4\AppPath (installation location) \\ Autogen\Default.XML

      Then search code: <CODE>J</CODE> and below this add following:

      <CLASS>
      <NAME>TEST</NAME>
      <GUID>111111111111111111</GUID>
      <WIDTH>4</WIDTH>
      <DEPTH>4</DEPTH>
      <LIBRARYOBJECT>
      <NAME>TESTOBJ</NAME>
      <GUID>2222222222222222222</GUID>
      </LIBRARYOBJECT>
      </CLASS>

      And save.

      I attach full code, one with and one without additional entry.

      Thanks for help,
      Tom
      Attached Files

      Comment


      • #4
        Please find attached a zip file with a Setup Factory 9 project file, the compiled project (sample.exe), and the original XML file you provided.

        First of all, before you begin, please see the linked page. Download and install the Encoding plugin, as you will need it before you can work with the project file.

        Once you installed the plugin on your system, please open the project file, and inspect the On Startup script, as everything is done there. Of course, in a real installer, you would perform these actions after the product files were deployed, so On Post Install would be more appropriate - but this is only a demonstration of the script itself.

        Step 1 shows that I attempt to locate the AppPath in the registry. I do not have this application installed, so instead I load the file "original.xml" from the same folder where the sample is being run, if the registry key cannot be found or if the file appointed by the retrieved information does not exist.

        Step 2 shows how to load the XML file. The file you provided is UTF-16 LE encoded (also known as Unicode). All actions in Setup Factory can work only on ANSI encoded files and strings, so I use my Encoding plugin to convert the file from Unicode to ANSI while it is being read and stored in a variable. Working on Unicode files directly will not work.

        Step 3 demonstrates how I first count the number of "REGION" elements in the XML. Once I have the total number of elements, I start a "for" loop to check all of them, until I find the one whose CODE is "J".

        Step 4 is where the new code is inserted into the XML. To make this more readable, the new XML code is stored in sXMLToInsert, defined at the beginning of the script.

        Step 5 is the last part, where the modified XML is saved into a new file. In this sample, I will not overwrite the original file, instead I save the new contents into a file named "modified.xml", in the same folder from where the application was run. Note that I use my plugin again here, to save the final file in Unicode encoding.

        If you now compare the "modified.xml" with the "default_withcode.xml" which you provided, you will see that the files match and that there are no differences. If you compare the "original.xml" with "modified.xml", you can see the insertion:

        Click image for larger version

Name:	SCRN-2017-07-16-01.png
Views:	177
Size:	69.3 KB
ID:	298017

        As you can see, Setup Factory is perfectly capable of making the changes you wish to perform. This is, however, not a trivial task for somebody who is just beginning to work with this product. For example, instead of simply adding the XML as in this sample, it would be advised to check if the code already exists, in order to avoid duplicate data. Also, if you uninstall, you would need to remove the inserted code, as otherwise the Flight Simulator may present issues.

        Ulrich
        Consulting Services and Software Extensions

        Comment


        • #5
          Hello,

          Thank you very much for your professional approach and answer on this.

          I found some troubles with accessing XML file, after pointing correct registry key I ended up with an error 'Could not find Default XML file at expected location!'.

          However I experimented and added SessionVar.Set("%AppFolder%", Registry.GetValue(HKEY_CURRENT_USER, "SOFTWARE\\Lockheed Martin\\Prepar3D v4", "AppPath", true)); on top of this (this will also do the job pointing correct installation path) and modified sXMLFileLocation = SessionVar.Expand("%AppFolder%\\Autogen\\default.x ml"); by adding %AppFolder% at the begging. It's working fine now.

          The file format also seems to be fine, it's working in the simulator. However I can't open it using XML Copy Editor, it returns 'file not well-formatted (invalid token)'.
          I downloaded Notepad++ and it opens without a problem.

          Regarding uninstall I though it may be worth to create default.xml backup during installation and when uninstalling bring it back.

          Thanks,
          Tom

          Comment


          • #6
            Hello,

            Does XML Copy Editor open the "default_withcode.xml" file without complaining? If it does, then I would try to compare the files, and look for differences, using a merge tool, or something else which would ignore whitespaces, line breaks and/or tabs, which do not alter the structure of the XML document. Or perhaps run the file through a code formatter before you compare them. You'll figure this out, if there is indeed an issue.

            Remember that the backup file approach will work only if there were no other custom add-ons installed (or removed) since you modified the XML.
            Good luck with your project!

            Ulrich

            Comment


            • #7
              Perfect, but how to delete that entry during uninstall ? Code !

              Comment


              • #8
                If you want "Code!", please contact me privately for consulting services, or at least show that you are a registered user of this product. I do not feel compelled to work for free.

                Ulrich

                Comment


                • #9
                  Originally posted by Ulrich View Post
                  If you want "Code!", please contact me privately for consulting services, or at least show that you are a registered user of this product. I do not feel compelled to work for free.

                  Ulrich
                  Actually i can't speak english very well ! but i would like to say that you are a great programmer, you are so brilliant and i am grateful to you, Ulrich.

                  Comment

                  Working...
                  X