Problem with InsertXMLStructure

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Problem with InsertXMLStructure

Post by Lebostein »

Here a simple code to convert a structure to a XML file:

Code: Select all

Structure set
  setID.s
  theme.s
EndStructure

Structure database
  List collection.set()
EndStructure

dummy.database

AddElement(dummy\collection())
dummy\collection()\setID = "1234"
dummy\collection()\theme = "Test"
AddElement(dummy\collection())
dummy\collection()\setID = "6688"
dummy\collection()\theme = "Auto"

*XML = CreateXML(#PB_Any)
InsertXMLStructure(RootXMLNode(*XML), dummy, database)
FormatXML(*XML, #PB_XML_ReFormat)
Debug ComposeXML(*XML)
This is the output:
<database>
....<collection>
........<element>
............<setID>1234</setID>
............<theme>Test</theme>
........</element>
........<element>
............<setID>6688</setID>
............<theme>Auto</theme>
........</element>
....</collection>
</database>
Why my list entries are marked with "<element>"? If I look to the naming of my structures I expect a <set>...</set> around my list entries... so it is impossible to read or write XML files with a fixed format. I have the full control over every word in this XML, but not over that "element" node name....
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Problem with InsertXMLStructure

Post by srod »

InsertXMLStructure() is recursive in the way it adds nodes for the various structure fields and so, from the user manual entry for InsertXMLList() :
The inserted node is named "list" and the contained element nodes are named "element".
Why not simply do a ForEach on the list and then InsertXMLStructure() etc.
I may look like a mule, but I'm not a complete ass.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Problem with InsertXMLStructure

Post by Lebostein »

srod wrote:InsertXMLStructure() is recursive in the way it adds nodes for the various structure fields and so, from the user manual entry for InsertXMLList() :
The inserted node is named "list" and the contained element nodes are named "element".
The user manual seems wrong. There is no node name "list" in my example. InsertXMLStructure uses my given list name "collection". And for the main node it uses my given structure name "database". It is incomprehensible to me why the structure name of the list elements is not used too. In my view, that would be logical and consistent.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Problem with InsertXMLStructure

Post by srod »

Makes kind of sense to me.

In the case of InsertXMLList(), there is no 'named' list/structure for PB to use and so the node is named, simply, 'list'. Naming the node to match a variable name doesn't really do it for me. In the case where a structure field consists of a list, we have a named field and so PB can (and does) name the node appropriately. As for the individual list element nodes; again fine when dealing with a list of structures, but what about a list of native types, e.g. a list of integers? Could name the nodes 'integer' instead of 'element' I guess - but that would be about as far as PB could go. A bit of a compromise, but certainly not incomprehensible especially when you can easily get around all of this.
I may look like a mule, but I'm not a complete ass.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Problem with InsertXMLStructure

Post by Lebostein »

My intention is to read/import given third party XML files in to a PB structure. My idea was to name my structures, variables and lists in the same way as the given XML. This works very well, but it fails at list elements.... my workaround at the moment is to rename all list elements to <element> before I parse the XML file...
Post Reply