Hollywood - The Multimedia Application Layer

  • up
    34%
  • down
    66%

Hello readers,

Over the following months I'll be posting some source code examples using my base:library which is available (as an older version which will be shortly updated) on OS4Depot.

The base:library contains many useful functions for creating:

The first installment is available now:

Extracting data from XML documents

What is XML? - Wikipedia link

So now you know what it is in essence how can we start making use of this format in our projects.
Well as a quick example, if you were creating a game you may want to have a high score table. You only require two fields: Name and Score.
It is entirely up to you how you lay this information out - however the file should always start with the ?xml and pobjects lines.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <pobjects version="1.0">
  3. <dict>
  4. <key>High Score Table</key>
  5. <array>
  6. <dict>
  7. <key>Name</key>
  8. <string>Richard Lake</string>
  9. <key>Score</key>
  10. <integer>12345678</integer>
  11. <key>Name</key>
  12. <string>Fred Bloggs</string>
  13. <key>Score</key>
  14. <integer>12345</integer>
  15. </dict>
  16. </array>
  17. </dict>
  18. </pobjects>

Your always start extraction by skipping over the first 3 lines, in this example we are only defining 1 array so we can safely skip over the first 7 lines to begin at the line:

  1. <key>Name</key>

We then define a While....Wend block to extract the information from the the table line by line. So In the example above we only need to make use of base:GetXMLString and base:GetXMLInteger within the loop.

XML documents can be as complex or simple as you wish, for example in application software you may wish the user to be able to save settings in different areas of the application. Instead of using multiple XML documents you can instead save everything into one single file and define an <array> block to each application area.

In the source code provided the While...Wend block only performs extraction top to bottom and line by line with no structural validation taking place. Under normal circumstances, the end user should not have to manually edit XML documents, but XML documents are inheritly plain text so can be easily opened and edited within a simple text editor.
So if you were doing a game with a high score table you'd probably want to compress or encrypt the XML document in someway as well - Hollywood provide functions for this as well which go outside the scope of this blog.

Okay here is the source code example, I hope you will find it of some use in your future projects. Thanks for reading.

Source code

You can find out more writing software for Hollywood on the official forums:
Hollywood forums

Blog post type: 

Comments

trixie's picture

@djrikki

the file should always start with the standard AmigaOS XML definition

Sorry for nitpicking but to avoid possible confusion: there's nothing like "standard AmigaOS XML definition", really. The format you're describing in your tutorial is the AmigaOS4 application.library's PrefsObjects XML document type. So the line should read "with the standard AmigaOS4 XML/PrefsObjects declaration" - because that's what the !DOCTYPE-line actually is.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

djrikki's picture

Hi trixie,

Yes I guess your right, infact one could say that line can be removed entirely if its not exclusively a 'prefs' file you are not dealing with. If I remember correctly I left it in place in my projects so that I could open them in the PrefsObjectEditor... however it would never work reporting something about 'duplicate IDs'. I guess I just left it in there.

Blog updated.

AmigaOS.net - Discover Amiga, Discover AmigaOS.net
Jack for AmigaOS
Samba Idiot's Guide

trixie's picture

@djrikki

I left it in place in my projects so that I could open them in the PrefsObjectEditor... however it would never work reporting something about 'duplicate IDs'

Now I can see it. You're getting this error from the PrefsObjectEditor because in your prefs dictionary, you have two keys called "Name" and two keys named "Score" - these are the "duplicate IDs". The PrefsObjects system requires unique key names because the key values are retrieved from the dictionary using the name. So ditching the doctype declaration in your particular example does make sense - the file could not be used with application.library anyway.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2