Could anyone give me a simple example of creating an xml file out of book information? I can't wrap my head around it. Basically, I want to export a series of records from a database which are library books.
title Robert's claw
publisher Jorgi Books
pages 287
binding paper
price $15.00
rating excellent
genre science fiction
series elder earth
XML example
- Fangbeast
- PureBasic Protozoa
- Posts: 4789
- Joined: Fri Apr 25, 2003 3:08 pm
- Location: Not Sydney!!! (Bad water, no goats)
XML example
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Code: Select all
Global *xml = 0
Structure Book
title.s
publisher.s
pages.l
binding.s
price.s
rating.s
genre.s
series.s
EndStructure
Procedure AddBook(*new.book)
Protected *nNode, *node, *root
Static ID
If *xml = 0
*xml = CreateXML(#PB_Any)
*root = CreateXMLNode(RootXMLNode(*xml))
SetXMLNodeName(*root, "list")
EndIf
ID + 1
*nNode = CreateXMLNode(MainXMLNode(*xml))
If *nNode
SetXMLNodeName(*nNode, "Books")
SetXMLAttribute(*nNode, "id", Str(ID))
*node = CreateXMLNode(*nNode)
With *new
SetXMLNodeName(*node, "title")
SetXMLNodeText(*node, \title)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "publisher")
SetXMLNodeText(*node, \publisher)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "pages")
SetXMLNodeText(*node, Str(\pages))
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "binding")
SetXMLNodeText(*node, \binding)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "price")
SetXMLNodeText(*node, \price)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "rating")
SetXMLNodeText(*node, \rating)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "genre")
SetXMLNodeText(*node, \genre)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "series")
SetXMLNodeText(*node, \series)
EndWith
EndIf
EndProcedure
Define.Book my
With my
\title = "Robert's claw"
\publisher = "Jorgi Books"
\pages = 287
\binding = "paper"
\price = "$15.00"
\rating = "excellent"
\genre = "science fiction"
\series = "elder earth"
EndWith
AddBook(@my)
FormatXML(*xml, #PB_XML_ReFormat | #PB_XML_WindowsNewline)
Define.s text = Space(ExportXMLSize(*xml))
ExportXML(*xml, @text, Len(text))
MessageRequester("Books", text)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

- zekitez@lycos.com
- User
- Posts: 15
- Joined: Fri Nov 11, 2005 5:42 pm
- Location: Netherlands
- Contact:
Re: XML example
The example seems to work just fine until you try to save the XML with SaveXML.
CreateXML(#PB_Any) doesn't return a pointer. At least not with version 4.41 .
CreateXML(#PB_Any) doesn't return a pointer. At least not with version 4.41 .
Code: Select all
Global xml = 0
Structure Book
title.s
publisher.s
pages.l
binding.s
price.s
rating.s
genre.s
series.s
EndStructure
Procedure AddBook(*new.book)
Protected *nNode, *node, *root
Static ID
If xml = 0
xml = CreateXML(#PB_Any)
*root = CreateXMLNode(RootXMLNode(xml))
SetXMLNodeName(*root, "list")
EndIf
ID + 1
*nNode = CreateXMLNode(MainXMLNode(xml))
If *nNode
SetXMLNodeName(*nNode, "Books")
SetXMLAttribute(*nNode, "id", Str(ID))
*node = CreateXMLNode(*nNode)
With *new
SetXMLNodeName(*node, "title")
SetXMLNodeText(*node, \title)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "publisher")
SetXMLNodeText(*node, \publisher)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "pages")
SetXMLNodeText(*node, Str(\pages))
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "binding")
SetXMLNodeText(*node, \binding)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "price")
SetXMLNodeText(*node, \price)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "rating")
SetXMLNodeText(*node, \rating)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "genre")
SetXMLNodeText(*node, \genre)
*node = CreateXMLNode(*nNode)
SetXMLNodeName(*node, "series")
SetXMLNodeText(*node, \series)
EndWith
EndIf
EndProcedure
Define.Book my
With my
\title = "Robert's claw"
\publisher = "Jorgi Books"
\pages = 287
\binding = "paper"
\price = "$15.00"
\rating = "excellent"
\genre = "science fiction"
\series = "elder earth"
EndWith
AddBook(@my)
FormatXML(xml, #PB_XML_ReFormat | #PB_XML_WindowsNewline)
Define.s text = Space(ExportXMLSize(xml))
ExportXML(xml, @text, Len(text))
MessageRequester("Books", text)
SaveXML(xml,"Books.xml")