Page 1 of 2

XML problem with my Config file

Posted: Sat Sep 21, 2024 8:04 pm
by GenRabbit
I'm trying to create a xml Config file, but I can't figure out how. Tried GPT4, but that was terrible. Basically its based on this one;
I already know that xml2 comes up with zero, but I do not know why.
The second code block is my original config file I want to convert to xml

Code: Select all

EnableExplicit
Global.s ReturnString
Global.i xml, mainNode, item, x, Xlm2
Global.w DefaultSettingImpaleIndex, DefaultSettingMeleeIndex, DefaultRQ2CritDamageIndex, DefaultSettingRangePenalty
Global.l LMAXCHARACTERCONTAINERS
Global.l HIDDENSKILLSMAX = 6
Global.w TimeDaysInWeek = 14

Structure HiddenSkillsStruct
    OnOff.w
    Skillid.l
EndStructure

NewList HSkills.HiddenSkillsStruct()
For x = 0 To 10
    AddElement(HSkills())
    HSkills()\Skillid = 1000 + x
    HSkills()\OnOff = 1
Next x

NewList NameOfMonth.s()
    AddElement(NameOfMonth())   
    NameOfMonth()="Solan"
    AddElement(NameOfMonth())   
    NameOfMonth()="Dellios"
    AddElement(NameOfMonth())   
    NameOfMonth()="Rande"
    AddElement(NameOfMonth())   
    NameOfMonth()="Varian"
    AddElement(NameOfMonth())   
    NameOfMonth()="Mosicante"
    AddElement(NameOfMonth())   
    NameOfMonth()="Uliande"
    AddElement(NameOfMonth())   
    NameOfMonth()="Horitan"
    AddElement(NameOfMonth())   
    NameOfMonth()="Belizadore"
    AddElement(NameOfMonth())   
    NameOfMonth()="Koloss"
    AddElement(NameOfMonth())   
    NameOfMonth()="Jarajan"
    AddElement(NameOfMonth())   
    NameOfMonth()="Polokk"
    AddElement(NameOfMonth())   
    NameOfMonth()="Quartani"
    AddElement(NameOfMonth())   
    NameOfMonth()="Romante"
    AddElement(NameOfMonth())   
    NameOfMonth()="Morvika"
NewList DaysInMonth.w()   
  AddElement(DaysInMonth())   
    DaysInMonth()=24
  AddElement(DaysInMonth())   
    DaysInMonth()=26
  AddElement(DaysInMonth())   
    DaysInMonth()=26
  AddElement(DaysInMonth())   
    DaysInMonth()=33
  AddElement(DaysInMonth())   
    DaysInMonth()=32
  AddElement(DaysInMonth())   
    DaysInMonth()=32
  AddElement(DaysInMonth())   
    DaysInMonth()=29
  AddElement(DaysInMonth())   
    DaysInMonth()=32
  AddElement(DaysInMonth())   
    DaysInMonth()=31
  AddElement(DaysInMonth())   
    DaysInMonth()=27
  AddElement(DaysInMonth())   
    DaysInMonth()=32
  AddElement(DaysInMonth())   
    DaysInMonth()=28
  AddElement(DaysInMonth())   
    DaysInMonth()=28
  AddElement(DaysInMonth())   
    DaysInMonth()=26    
    
; Create xml tree
  xml = CreateXML(#PB_Any,#PB_UTF8) 
  item = CreateXMLNode(RootXMLNode(xml), "Time") 
  
  ;Create first xml node (in main node)
  SetXMLAttribute(item, "MinutesFromTheBeginning", "233335017") 
  SetXMLAttribute(Item, "GameLanguage", "2") 
  SetXMLAttribute(Item, "RuleSet_ImpaleValue", StrU(DefaultSettingImpaleIndex,#PB_Word)) 
  SetXMLAttribute(Item,  "RuleSet_MeleeDBLDamage", StrU(DefaultSettingMeleeIndex,#PB_Word)) 
  SetXMLAttribute(Item, "MaxCharContainers", StrU(LMAXCHARACTERCONTAINERS,#PB_Long)) 
  SetXMLAttribute(Item, "RQ2CrtitcalDamage", StrU(DefaultRQ2CritDamageIndex,#PB_Word)) 
  SetXMLAttribute(Item, "DefaultRangeSetting", StrU(DefaultSettingRangePenalty,#PB_Word))
  If HIDDENSKILLSMAX
      Xlm2 = CreateXMLNode(xml,"HiddenSkills",0)
      Debug "Inserted List"
      Debug Xlm2
      InsertXMLList(Xlm2, HSkills(),#PB_XML_Normal)
  EndIf
  
  SetXMLAttribute(Item, "MinutesInHour", "2") 
  SetXMLAttribute(Item, "HourInDay", "2") 
  SetXMLAttribute(Item, "MonthsInYear", "2") 

  SetXMLAttribute(Item, "DaysInWeek", "2") 
  SetXMLAttribute(Item, "NameOfMonths", "2") 
  SetXMLAttribute(Item, "DaysInMonths", "2") 
  FormatXML(xml,#PB_XML_ReFormat|#PB_XML_WindowsNewline,0)
  ; Save the xml tree into a xml file
  Debug ComposeXML(xml)
  SaveXML(xml, "e:\temp\demo.xml")
  FreeXML(xml)

Code: Select all

GameLanguage=1
RuleSet_ImpaleValue=1
RuleSet_MeleeDBLDamage=1
MaxCharContainers=60
RQ2CrtitcalDamage=0
DefaultRangeSetting=1
HiddenSkill=3001000
HiddenSkill=3002000
HiddenSkill=3051000
HiddenSkill=3051010
HiddenSkill=3051007
HiddenSkill=5001000
HiddenSkill=5002000
HiddenSkill=5003000
HiddenSkill=5004000
HiddenSkill=6001000
HiddenSkill=6002000
MinutesFromTheBeginning=233335017
MinutesInHour=60
HourInDay=24
MonthsInYear=14
NameOfMonths=Solan,Dellios,Rande,Varian,Mosicante,Uliande,Horitan,Belizadore,Koloss,Jarajan,Polokk,Quartani,Romante,Morvika
DaysInMonths=24,26,26,33,32,32,29,32,31,27,32,28,28,26
DaysInWeek=8
NameOfDays=Morrasday,Nirmoday,Yranday,Janmarday,Turenday,Ermaday,Ulvakday,Cortenday

Re: XML problem with my Config file

Posted: Sat Sep 21, 2024 8:23 pm
by jacdelad
1. Don't use ChatGPT and such. The results are #+!$&.
2. I repeat: Don't use them. Use the help and the forum.
3. Does it necessarily have to be XML? PureBasic has a built-in library called "Preferences", which could suit you too.

Re: XML problem with my Config file

Posted: Sat Sep 21, 2024 8:35 pm
by GenRabbit
It doesn't have to, but I was curious if it would work, and how it works.
Chatgpt 4.0 wrote three versions for it, everytime using commands Purebasic didn't have. So that was a door in the face.
never heard of Preferances.

Re: XML problem with my Config file

Posted: Sat Sep 21, 2024 8:37 pm
by jacdelad
https://www.purebasic.com/documentation ... index.html

The opinions about ChatGPT and such differ heavily here. From my point of view it usually does not work and may teach you bad programming beahviours...

Re: XML problem with my Config file

Posted: Sat Sep 21, 2024 8:42 pm
by GenRabbit
Jupp, chatgpt 4.0 is terrible. But you think Prefreance is better than xml?

Re: XML problem with my Config file

Posted: Sat Sep 21, 2024 8:47 pm
by jacdelad
Depends on what exactly you need:

Are the preferences just for you or do they have to be "universal"?
Do you just store simple values or complex ones (with line breaks and such)?
Maybe you have to store more than just text?
...

After considering this you can either use Preferences (simple storage), XML (more structured and better comptibility to other software), a structure (space saving and flexible if only needed by you), and so on...

Really, depends on your needs.

Re: XML problem with my Config file

Posted: Sat Sep 21, 2024 10:38 pm
by Fred
If you own the format of your XML, you can use Insertxmlstructure() https://www.purebasic.com/documentation ... cture.html which easily does all the work for you and is very PB friendly.

Re: XML problem with my Config file

Posted: Sun Sep 22, 2024 5:24 am
by Demivec
GenRabbit wrote: Sat Sep 21, 2024 8:04 pm I already know that xml2 comes up with zero, but I do not know why.
I just wanted to address this question.

Without running or testing your code I think the error lies in specifying where you were trying to create the node.

The code creates nodes with the following structure:

Code: Select all

---* [root node]
         \
          xml2 ---* [normal node]  <<<<< node creation failed 
         \
          item ---* [normal node] "Time" with attributes
xml2 failed to be created because you used the XML object ID# xml instead of a node identifier and also because it would have been the second 'normal' type node to be created in the root node (if my presumption is correct that you had intended to use 'RootXMLNode(xml))' instead of 'xml' by itself to specify the parent node).

You could first create a normal node in the root, called 'top', and then create both nodes item and xml2 as a child of that node.

The structure would resemble this:

Code: Select all

--- * [root node]
          \
           top ---* [normal node]
                   \
                    xml2 ---* [normal node]
                   \
                    item ---* [normal node] "Time" with attributes
The code that creates just the XML tree might look like this (you would so have to add the structure and variable definitions at the beginning of the code):

Code: Select all

; Create xml tree
  Global.i top
  xml = CreateXML(#PB_Any,#PB_UTF8) 
  top = CreateXMLNode(RootXMLNode(xml)) 
  item = CreateXMLNode(top, "Time") 
  
  ;Create first xml node (in top level normal node)
  SetXMLAttribute(item, "MinutesFromTheBeginning", "233335017") 
  SetXMLAttribute(Item, "GameLanguage", "2") 
  SetXMLAttribute(Item, "RuleSet_ImpaleValue", StrU(DefaultSettingImpaleIndex,#PB_Word)) 
  SetXMLAttribute(Item,  "RuleSet_MeleeDBLDamage", StrU(DefaultSettingMeleeIndex,#PB_Word)) 
  SetXMLAttribute(Item, "MaxCharContainers", StrU(LMAXCHARACTERCONTAINERS,#PB_Long)) 
  SetXMLAttribute(Item, "RQ2CrtitcalDamage", StrU(DefaultRQ2CritDamageIndex,#PB_Word)) 
  SetXMLAttribute(Item, "DefaultRangeSetting", StrU(DefaultSettingRangePenalty,#PB_Word))
  If HIDDENSKILLSMAX
      Xlm2 = CreateXMLNode(top,"HiddenSkills",0)
      Debug "Inserted List"
      Debug Xlm2
      InsertXMLList(Xlm2, HSkills(),#PB_XML_Normal)
  EndIf
  
  SetXMLAttribute(Item, "MinutesInHour", "2") 
  SetXMLAttribute(Item, "HourInDay", "2") 
  SetXMLAttribute(Item, "MonthsInYear", "2") 

  SetXMLAttribute(Item, "DaysInWeek", "2") 
  SetXMLAttribute(Item, "NameOfMonths", "2") 
  SetXMLAttribute(Item, "DaysInMonths", "2") 
  FormatXML(xml,#PB_XML_ReFormat|#PB_XML_WindowsNewline,0)
  Debug ComposeXML(xml)

Re: XML problem with my Config file

Posted: Sun Sep 22, 2024 8:35 am
by BarryG
jacdelad wrote: Sat Sep 21, 2024 8:23 pmDon't use ChatGPT
ChatGPT can be useful if you ask it for the theory instead of the practice ("how is this done" instead of "code this for me"). It's pointed me in the right direction before so that I didn't have to post here and wait for an answer, so it's not all bad.

Re: XML problem with my Config file

Posted: Sun Sep 22, 2024 2:20 pm
by infratec
What Fred means ...

Code: Select all

Structure Ini_Structure
  GameLanguage.i
  RuleSet_ImpaleValue.i
  RuleSet_MeleeDBLDamage.i
  MaxCharContainers.i
  RQ2CrtitcalDamage.i
  DefaultRangeSetting.i
  List HiddenSkill.i()
  MinutesFromTheBeginning.i
  MinutesInHour.i
  HourInDay.i
  MonthsInYear.i
  List NameOfMonths.s()
  List DaysInMonths.i()
  DaysInWeek.i
  List NameOfDays.s()
EndStructure


Define Ini.Ini_Structure

Ini\GameLanguage=1
Ini\RuleSet_ImpaleValue=1
Ini\RuleSet_MeleeDBLDamage=1
Ini\MaxCharContainers=60
Ini\RQ2CrtitcalDamage=0
Ini\DefaultRangeSetting=1

AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=3001000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=3002000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=3051000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=3051010
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=3051007
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=5001000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=5002000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=5003000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=5004000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=6001000
AddElement(Ini\HiddenSkill())
Ini\HiddenSkill()=6002000

Ini\MinutesFromTheBeginning=233335017
Ini\MinutesInHour=60
Ini\HourInDay=24
Ini\MonthsInYear=14

AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Solan"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Dellios"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Rande"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Varian"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Mosicante"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Uliande"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Horitan"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Belizadore"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Koloss"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Jarajan"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Polokk"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Quartani"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Romante"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Morvika"

AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=24
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=26
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=26
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=33
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=29
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=31
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=27
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=28
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=28
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=26

Ini\DaysInWeek=8

AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Morrasday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Nirmoday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Yranday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Janmarday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Turenday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Ermaday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Ulvakday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Cortenday"

If CreateXML(0)
  InsertXMLStructure(RootXMLNode(0), @Ini, Ini_Structure)
  FormatXML(0, #PB_XML_ReFormat)
  Debug ComposeXML(0)
EndIf
You can do the same as a JSON:

Code: Select all

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @Ini, Ini_Structure)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf

Re: XML problem with my Config file

Posted: Sun Sep 22, 2024 11:27 pm
by GenRabbit
You can do the same as a JSON:

Code: Select all

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @Ini, Ini_Structure)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
First thanks all. I tried out this code, also the Json version, which I liked the most. XML seems to generate an awful lot of overhead for a small file.
I need to check this out more.
For the moment I've gone with preferance. Didnt see this before 20 hours later or so.
I noticed that both used just one element per line. Can you not under neither XML or JSON have something like;
Mydata=alpha,bravo,charlie,delta?

Re: XML problem with my Config file

Posted: Mon Sep 23, 2024 12:45 am
by jacdelad
Well...you are the programmer. As long as it's conform to XML, it's up to you.

Re: XML problem with my Config file

Posted: Mon Sep 23, 2024 4:10 pm
by Axolotl
Hi,

what you see is perhaps because of the optional Flag. See Help for further information:
Flags (optional) If set to #PB_JSON_PrettyPrint, the composed string will contain additional newline and whitespace for better readability. The extra whitespace is not significant to the JSON format. The output will have the same meaning to a JSON reader with or without this flag.
Conclusion: If you (your users) create the file by hand, they simply have to follow the syntax rules.

Re: XML problem with my Config file

Posted: Mon Nov 11, 2024 10:54 pm
by GenRabbit
I've been playing around with the JSON/XML, and I noticed XML seems to write the data out the way its added, first in is first written to file, but when using JSON, the values comes out of order? how come?

with xml, LIsts are at the end-

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

<Ini_Structure>
  <ServerLanguage>1</ServerLanguage>
  <RuleSet_ImpaleValue>1</RuleSet_ImpaleValue>
  <RuleSet_MeleeDBLDamage>1</RuleSet_MeleeDBLDamage>
  <MaxCharContainers>60</MaxCharContainers>
  <RQ2CrtitcalDamage>0</RQ2CrtitcalDamage>
  <DefaultRangeSetting>1</DefaultRangeSetting>
  <MinutesFromTheBeginning>233335017</MinutesFromTheBeginning>
  <MonthsInYear>14</MonthsInYear>
  <HourInDay>24</HourInDay>
  <MinutesInHour>60</MinutesInHour>
  <DaysInWeek>8</DaysInWeek>
  <NameOfDays>
    <element>Morrasday</element>
    <element>Nirmoday</element>
    <element>Yranday</element>
    <element>Janmarday</element>
    <element>Turenday</element>
    <element>Ermaday</element>
    <element>Ulvakday</element>
    <element>Cortenday</element>
  </NameOfDays>
  <HiddenSkill>
    <element>3001000</element>
    <element>3002000</element>
    <element>3051000</element>
    <element>3051010</element>
    <element>3051007</element>
    <element>5001000</element>
    <element>5002000</element>
    <element>5003000</element>
    <element>5004000</element>
    <element>6001000</element>
    <element>6002000</element>
  </HiddenSkill>
  <NameOfMonths>
    <element>Solan</element>
    <element>Dellios</element>
    <element>Rande</element>
    <element>Varian</element>
    <element>Mosicante</element>
    <element>Uliande</element>
    <element>Horitan</element>
    <element>Belizadore</element>
    <element>Koloss</element>
    <element>Jarajan</element>
    <element>Polokk</element>
    <element>Quartani</element>
    <element>Romante</element>
    <element>Morvika</element>
  </NameOfMonths>
  <DaysInMonths>
    <element>24</element>
    <element>26</element>
    <element>26</element>
    <element>33</element>
    <element>32</element>
    <element>32</element>
    <element>29</element>
    <element>32</element>
    <element>31</element>
    <element>27</element>
    <element>32</element>
    <element>28</element>
    <element>28</element>
    <element>26</element>
  </DaysInMonths>
</Ini_Structure>
in JSON, its more random

Code: Select all

{
  "MaxCharContainers"      : 60,
  "DaysInWeek"             : 8,
  "HourInDay"              : 24,
  "DefaultRangeSetting"    : 1,
  "NameOfMonths"           : [
      "Solan",
      "Dellios",
      "Rande",
      "Varian",
      "Mosicante",
      "Uliande",
      "Horitan",
      "Belizadore",
      "Koloss",
      "Jarajan",
      "Polokk",
      "Quartani",
      "Romante",
      "Morvika"
    ],
  "ServerLanguage"         : 1,
  "RQ2CrtitcalDamage"      : 0,
  "RuleSet_MeleeDBLDamage" : 1,
  "HiddenSkill"            : [
      3001000,
      3002000,
      3051000,
      3051010,
      3051007,
      5001000,
      5002000,
      5003000,
      5004000,
      6001000,
      6002000
    ],
  "MinutesInHour"          : 60,
  "RuleSet_ImpaleValue"    : 1,
  "DaysInMonths"           : [
      24,
      26,
      26,
      33,
      32,
      32,
      29,
      32,
      31,
      27,
      32,
      28,
      28,
      26
    ],
  "MonthsInYear"           : 14,
  "MinutesFromTheBeginning": 233335017,
  "NameOfDays"             : [
      "Morrasday",
      "Nirmoday",
      "Yranday",
      "Janmarday",
      "Turenday",
      "Ermaday",
      "Ulvakday",
      "Cortenday"
    ]
}
And thanks for the help

Re: XML problem with my Config file

Posted: Mon Nov 11, 2024 11:32 pm
by Kiffi
In JSON, the order of object elements is irrelevant.

In this respect, you would have to restructure your XML a little.

Instead of:

Code: Select all

<NameOfMonths>
	<element>Solan</element>
	<element>Dellios</element>
	[and so on...]
</NameOfMonths>
<DaysInMonths>
	<element>24</element>
	<element>26</element>
	[and so on...]
</DaysInMonths>
I would suggest the following:

Code: Select all

<Months>
	<Month>
		<Name>Solan</Name>
		<AmountOfDays>24</AmountOfDays>
	</Month>
	<Month>
		<Name>Dellios</Name>
		<AmountOfDays>26</AmountOfDays>
	</Month>
	[and so on...]
</Months>