XML example

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

XML example

Post by Fangbeast »

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
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

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.
Image
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

Thanks, time to lay and not worry about fire.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
zekitez@lycos.com
User
User
Posts: 15
Joined: Fri Nov 11, 2005 5:42 pm
Location: Netherlands
Contact:

Re: XML example

Post by zekitez@lycos.com »

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 .

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")
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: XML example

Post by srod »

Works fine here with PB 4.41.
I may look like a mule, but I'm not a complete ass.
Post Reply