Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] easy-bake XML

From: norsepower::uswest::net at: 10-Nov-2000 22:40

Here is an XML translator I whipped up in just a couple of minutes. I am surprised how easy it is to work with XML and REBOL. More experienced REBOLers may find my code a bit clunky, but here it is... - - - - - - - - - - XML Grammar - Standard Message <author>Ryan C. Christiansen</author> <subject>This is the Subject of the Message</subject> <date>This is the date the message was created</date> <content>This is the main body of the message</content> <messageID>999999999999</messageID> - - - - - - - - - - Usage: translate-message/markup-data message where "message" is an object in the following format message: make object! [ author: "Ryan C. Christiansen" subject: "This is the Subject of the Message" date: 27-Oct-2000/17:27:43-5:00 content: {This is the main body of the message} messageID: 20001027172734 ] returns the data marked up in the XML grammar - - - - - translate-message/read-markup %file where %file is the name of a file containing data in the aforementioned XML grammar returns the contents of the file as a message object! - - - - - Here's the translator code... translate-message: make object! [ xml-tags: [ ["author" "/author"] ["subject" "/subject"] ["date" "/date"] ["content" "/content"] ["messageID" "/messageID"] ] markup-data: func [ data [object!] ][ data-object: make data [] object-data: next first data-object output: copy "" for x 1 (length? xml-tags) 1 [ item: reform [rejoin ["data-object" "/" (first object-data)]] made-tag: rejoin ["" (build-tag [(xml-tags/1/1)]) (do item) (build-tag [(xml-tags/1/2)])] xml-tags: next xml-tags object-data: next object-data append output made-tag ] xml-tags: head xml-tags output ] read-markup: func [ xml-file [file!] ][ message-data: load/markup xml-file xml-data: make object! [ author: message-data/<author> subject: message-data/<subject> date: message-data/<date> content: message-data/<content> messageID: message-data/<messageID> ] xml-data ] ]