Page 1 of 1

XML example

Posted: Mon Feb 23, 2009 4:47 am
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

Posted: Mon Feb 23, 2009 6:36 am
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)

Posted: Mon Feb 23, 2009 6:48 am
by Fangbeast
Thanks, time to lay and not worry about fire.

Re: XML example

Posted: Sun Mar 07, 2010 11:21 am
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")

Re: XML example

Posted: Sun Mar 07, 2010 12:04 pm
by srod
Works fine here with PB 4.41.