BaCon Mini-XML
Dec 9, 2012 7:13:52 GMT 1
Post by Deleted on Dec 9, 2012 7:13:52 GMT 1
Armando (AIR) created a Mini-XML example to show how I could parse large XML files in the Linux version of BCX. This eventually ended up being an extension module for ScriptBasic. With Peter's new C support, getting the Mini-XML library working with BaCon shouldn't be too hard. Any of you BaCon pros want to take a shot at it?
BCX Example
ScriptBasic Example
BCX Example
$execon "-lmxml"
$nomain
$HEADER
#include <mxml.h>
typedef mxml_node_t* XMLROOT;
typedef XMLROOT XMLNODE;
typedef mxml_index_t* XMLINDEX;
$HEADER
FUNCTION main(argc as INTEGER, argv as PCHAR PTR) as INTEGER
dim tree as XMLROOT, node as XMLNODE, sub_node as XMLNODE,ind as XMLINDEX
dim category$, value$
OPEN "residential.xml" FOR INPUT AS xmlFile
tree = mxmlLoadFile(NULL, xmlFile, MXML_OPAQUE_CALLBACK)
CLOSE xmlFile
ind = mxmlIndexNew(tree, NULL,NULL)
mxmlIndexReset(ind)
for integer cnt = 0 to ind->num_nodes
node = mxmlIndexEnum(ind)
if node = NULL then iterate
sub_node = mxmlWalkNext(node, tree,MXML_DESCEND_FIRST)
category$ = trim$(node->value.opaque$)
value$ = trim$(sub_node->value.opaque$)
if value$ != NUL$ then print category$;" = ";value$
next
mxmlIndexDelete(ind)
mxmlDelete(tree)
END FUNCTION
ScriptBasic Example
IMPORT mxml.bas
doc = mxml::LoadDoc("simple.xml")
node = mxml::GetNode(doc,"/breakfast_menu")
child = mxml::GetChild(node)
WHILE child
node = mxml::GetNode(child,"name")
if node then PRINT "name = ",mxml::GetNodeValue(node),"\n"
node = mxml::GetNode(child,"price")
if node THEN PRINT "price = ",mxml::GetNodeValue(node),"\n"
node = mxml::GetNode(child,"description")
if node THEN PRINT "description = ",mxml::GetNodeValue(node),"\n"
node = mxml::GetNode(child,"calories")
if node THEN PRINT "calories = ",mxml::GetNodeValue(node),"\n"
child = mxml::GetNext(child)
WEND
IF doc THEN mxml::FreeDoc(doc)