ARSA Language  2.8
(Encryption, GPU and Web 3.0)
xml Class Reference

#include <xml.h>

Public Member Functions

int xml (const char *filename)
 loading xml from file More...
 
int xml (const char *str)
 loading xml from buffer More...
 
int json (const char *filename)
 loading json from file More...
 
int json (const char *str)
 loading json from buffer More...
 
bool save (int doc_id, const char *filename)
 saving xml to file More...
 
bool save_json (int doc_id, const char *filename)
 saving json to file More...
 
bool close (int doc_id)
 close and free memory xml document functions: xml(), xml.json() and xml.new() from. More...
 
bool close_node (int node_id)
 close and free memory xml node. More...
 
bool close_array (int array_id)
 close and free memory xml nodes array functions: xml.get_node_array() from. More...
 
int first (int doc_id)
 get xml root node from doc id.
More...
 
int first_child (int node_id)
 get xml root node from node id.
More...
 
int next_child (int node_id)
 get next xml child in this node id.
More...
 
int get_node (int node_id, const char *node_name)
 get first xml child from all childs in this node id.
More...
 
int get_array (int node_id, const char *node_name)
 get all xml childs with same name from this node id.
MUST call xml.close_array() after used for memory free. More...
 
const char * get_name (int node_id)
 get name of this node id More...
 
const char * get_value (int node_id)
 get value of this node id More...
 
const char * get_att (int node_id, const char *att_name)
 get value from attribute of this node id More...
 
int new ()
 allocate empty xml document.
More...
 
int new_node (int root_id, const char *node_name, const char *value="")
 add xml node with a name to root node and return new node as id.
More...
 
bool new_att (int node_id, const char *att_name, const char *att_value)
 add attribute to xml node by attribute name and value. More...
 
int add_child (int doc_id, const char *node_name)
 add xml node with a name to document node and return new node as id.
More...
 
int insert_child (int root_id, int child_id)
 insert xml node to another root node and return new node as id.
More...
 
bool set_value (int node_id, const char *value)
 set value to xml node. More...
 
bool set_att_value (int node_id, const char *att_name, const char *att_value)
 set attribute value to this node id. More...
 
int get_node_count (int node_id, const char *node_name)
 get node count of this xml node.
More...
 
int get_array_count (int array_id)
 get array count.
More...
 
int get_array_element (int array_id, int index)
 get xml node from array with index.
More...
 
void for (int array_id, void *function)
 get xml node from array with for loop.
More...
 
const char * value (int node_id, const char *path)
 get value of this path, use last path as element name More...
 
int count (int node_id, const char *path)
 get array count from path.
More...
 
int array (int node_id, const char *path)
 get all xml childs with same path, use last path as element name.
MUST call xml.close_array() after used for memory free.
Example get at: xml.value() More...
 
const char * array_value (int array_id, int array_num, const char *path)
 get value in array follow path More...
 
void log (int id, const char *option="xml")
 debug and print xml or json buffer in log window More...
 
void logarray (int array_id, const char *option="xml")
 debug and print array of xml or json buffer in log window More...
 
const char * tostring (int doc_id)
 saving xml to string buffer More...
 
const char * tostringjson (int doc_id)
 saving json to string buffer More...
 
bool fset (const char *path, const char *value, const char *filename, const char *option="xml", bool push_back=false)
 set value to path at file immediately. More...
 
const char * fget (const char *path, const char *filename, const char *option="xml")
 get value from path in file immediately. More...
 
int fcount (const char *path, const char *filename, const char *option="xml")
 get path count in file immediately. More...
 
bool ferase (const char *path, const char *filename, const char *option="xml")
 erase path in file immediately. More...
 
bool fsave (const char *src_filename, const char *src_option, const char *dest_filename, const char *dest_option)
 convert file format to another file format. More...
 

Detailed Description

Definition at line 15 of file xml.h.

Constructor & Destructor Documentation

◆ xml() [1/2]

int xml::xml ( const char *  filename)

loading xml from file

Parameters
filenamename of xml file want to operator
Returns
id >= 0 is successed, -1 is failed.
Must call xml.close() for free memory.

Example:
xml("hello.xml") // loading same script folder
xml("/sdcard/members.xml") // loading from android memory card
xml("C:/Program Files (x86)/log.xml") // loading from win32 program file folder

◆ xml() [2/2]

int xml::xml ( const char *  str)

loading xml from buffer

Parameters
strxml syntax buffer that want to operator
Returns
id >= 0 is successed, -1 is failed.
Must call xml.close() for free memory.
XML Syntax:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from test="hello world">Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Example: XML Loading from buffer.
set($xml, xml(\"<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from test="hello world">Jani</from> <heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"\) ) // loading xml from buffer
// do xml stuff
xml.close($xml) // close pointer

Example: Open XML from buffer and write to file.
set($buf, \"<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from test="hello world">Jani</from> <heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"\) // prepare buffer
set($xml, xml($buf) ) // loading xml from buffer
xml.save($xml, "buf.xml") // save to file
xml.close($xml) // close pointer

Member Function Documentation

◆ add_child()

int xml::add_child ( int  doc_id,
const char *  node_name 
)

add xml node with a name to document node and return new node as id.

Parameters
doc_idid of doc node, doc_id from functions: xml(), xml.json() and xml.new()
node_namenew name of xml node
Returns
new node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ array()

int xml::array ( int  node_id,
const char *  path 
)

get all xml childs with same path, use last path as element name.
MUST call xml.close_array() after used for memory free.
Example get at: xml.value()

Parameters
node_idid of xml node
pathpath in xml or json that you want read value.
Returns
all nodes founded as array, id >= 0 is successed, -1 is failed

◆ array_value()

const char* xml::array_value ( int  array_id,
int  array_num,
const char *  path 
)

get value in array follow path

Parameters
array_idid of array xml node
array_numarray slot
pathpath inside array xml or json that you want read value.
Returns
value of node, not empty strings is successed, otherwise is failed

◆ close()

bool xml::close ( int  doc_id)

close and free memory xml document functions: xml(), xml.json() and xml.new() from.

Parameters
doc_idid of xml document, doc_id from functions: xml(), xml.json() and xml.new()
Returns
true is successed, false is failed

◆ close_array()

bool xml::close_array ( int  array_id)

close and free memory xml nodes array functions: xml.get_node_array() from.

Parameters
array_idid of array nodes
Returns
true is successed, false is failed

◆ close_node()

bool xml::close_node ( int  node_id)

close and free memory xml node.

Parameters
node_idid of xml node
Returns
true is successed, false is failed

◆ count()

int xml::count ( int  node_id,
const char *  path 
)

get array count from path.

Parameters
node_idid of xml node
pathpath in xml or json that you want read value.
Returns
successed is array count, 0 is not found, error is -1.

◆ fcount()

int xml::fcount ( const char *  path,
const char *  filename,
const char *  option = "xml" 
)

get path count in file immediately.

Parameters
pathpath that you want to operate.
filenamename of file that you want to operate.
optionfile type xml or json. default: xml.
Returns
path count.

Example:
set( $count0, xml.fcount(test/data/file/d3, hello.xml )) // get path count in hello.xml.
set( $count1, xml.fcount(test/data/file, hello.txt, json)) // get path count in hello.txt as json format.

◆ ferase()

bool xml::ferase ( const char *  path,
const char *  filename,
const char *  option = "xml" 
)

erase path in file immediately.

Parameters
pathpath that you want to operate.
filenamename of file that you want to operate.
optionfile type xml or json. default: xml.
Returns
successed return true, otherwise false.

Example:
xml.ferase(test/data/file/d3, hello.xml ) // erase path in hello.xml.
xml.ferase(test/data/file[0], hello.txt, json) // erase path array in hello.txt as json format.

◆ fget()

const char* xml::fget ( const char *  path,
const char *  filename,
const char *  option = "xml" 
)

get value from path in file immediately.

Parameters
pathpath that you want to operate.
filenamename of file that you want to operate.
optionfile type xml or json. default: xml.
Returns
successed return value as string, otherwise is empty.

Example:
set( $value0, xml.fget(test/data/file/d3, hello.xml )) // get value from path in hello.xml.
set( $value1, xml.fget(test/data/file/d3/d4, hello.xml )) // get value from path in hello.xml.
set( $value2, xml.fget(test/data/file/d3, hello.txt, json)) // get value from path in hello.txt as json format.
set( $value3, xml.fget(test/data/file/d3/d4, hello.txt, json )) // get value from path in hello.txt as json format.
set( $value4, xml.fget(test/data/file[0], hello.txt, json)) // get array first, get value from path in hello.txt as json format.
set( $value5, xml.fget(test/data/file[1], hello.txt, json)) // get array second , get value from path in hello.txt as json format.

◆ first()

int xml::first ( int  doc_id)

get xml root node from doc id.

Parameters
doc_idid of xml document, doc_id from functions: xml(), xml.json() and xml.new()
Returns
root node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ first_child()

int xml::first_child ( int  node_id)

get xml root node from node id.

Parameters
node_idid of xml node
Returns
root node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ for()

void xml::for ( int  array_id,
void *  function 
)

get xml node from array with for loop.

Parameters
array_idid of xml array from function: xml.get_node_array()
functioncall the function until end loop.
Automatic built-in index loop each, can using "null" with function: xml.get_array_element()
Using break() or return() to stop them.

◆ fsave()

bool xml::fsave ( const char *  src_filename,
const char *  src_option,
const char *  dest_filename,
const char *  dest_option 
)

convert file format to another file format.

Parameters
src_filenamename of source file that you want to operate.
src_optionsource file type xml or json.
dest_filenamename of destination file that you want to save as a new file.
dest_optiondestination file type xml or json.
Returns
successed return true, otherwise false.

Example:
xml.fsave( hello.txt, json, hello.xml, xml ) // convert json to xml follow src and dest filename.

◆ fset()

bool xml::fset ( const char *  path,
const char *  value,
const char *  filename,
const char *  option = "xml",
bool  push_back = false 
)

set value to path at file immediately.

Parameters
pathpath that you want to operate.
valuea value that want set to path.
filenamename of file that you want to operate.
optionfile type xml or json. default: xml.
push_backtrue is push_back or false is overwrite. default: false (mean overwrite).
Returns
successed return true, otherwise false.

Example:
xml.fset(test/data/file/d3, combo.mp4, hello.xml ) // set value is combo.mp4 to path in hello.xml.
xml.fset(test/data/file/d3/d4, hello, hello.xml ) // set value is hello to path in hello.xml.
xml.fset(test/data/file/d3, combo.mp4, hello.txt, json) // set value is combo.mp4 to path in hello.txt as json format.
xml.fset(test/data/file/d3/d4, hello world, hello.txt, json ) // set value is hello world to path in hello.txt as json format.
xml.fset(test/data/file, music, hello.txt, json, true) // push_back first time, value is music to path in hello.txt as json format.
xml.fset(test/data/file, music, hello.txt, json, true) // push_back second time, value is music to path in hello.txt as json format.

◆ get_array()

int xml::get_array ( int  node_id,
const char *  node_name 
)

get all xml childs with same name from this node id.
MUST call xml.close_array() after used for memory free.

Parameters
node_idid of xml node
node_namename of xml node that want to operator
Returns
all nodes founded as array, id >= 0 is successed, -1 is failed

◆ get_array_count()

int xml::get_array_count ( int  array_id)

get array count.

Parameters
array_idid of xml array from functions: xml.get_node_array()
Returns
array count

◆ get_array_element()

int xml::get_array_element ( int  array_id,
int  index 
)

get xml node from array with index.

Parameters
array_idid of xml array from function: xml.get_node_array()
indexarray index, do not over array size-1.
May be size from function: xml.get_array_count()
This parameter can be "null" using inside function: xml.for()
Returns
new node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ get_att()

const char* xml::get_att ( int  node_id,
const char *  att_name 
)

get value from attribute of this node id

Parameters
node_idid of xml node
att_namename of xml attribute that want to operator
Returns
name of attribute, not empty strings is successed, otherwise is failed

◆ get_name()

const char* xml::get_name ( int  node_id)

get name of this node id

Parameters
node_idid of xml node
Returns
value of node, not empty strings is successed, otherwise is failed

◆ get_node()

int xml::get_node ( int  node_id,
const char *  node_name 
)

get first xml child from all childs in this node id.

Parameters
node_idid of xml node
node_namename of xml node that want to operator
Returns
first node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ get_node_count()

int xml::get_node_count ( int  node_id,
const char *  node_name 
)

get node count of this xml node.

Parameters
node_idid of xml node
node_namename of xml node that want to operator, empty node_name is get all node
Returns
node count

◆ get_value()

const char* xml::get_value ( int  node_id)

get value of this node id

Parameters
node_idid of xml node
Returns
value of node, not empty strings is successed, otherwise is failed

◆ insert_child()

int xml::insert_child ( int  root_id,
int  child_id 
)

insert xml node to another root node and return new node as id.

Parameters
root_idid of root xml node
child_idid of child xml node
Returns
new node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ json() [1/2]

int xml::json ( const char *  filename)

loading json from file

Parameters
filenamename of json file want to operator
Returns
id >= 0 is successed, -1 is failed.
Must call xml.close() for free memory.

Example:
xml.json("hello.json") // loading json file in same folder of script.
xml.json("/sdcard/members.json") // loading json from android memory card.
xml.json("C:/Program Files (x86)/log.json") // loading json from win32 program file folder.

◆ json() [2/2]

int xml::json ( const char *  str)

loading json from buffer

Parameters
strjson syntax buffer that want to operator
Returns
id >= 0 is successed, -1 is failed.
Must call xml.close() for free memory.
json syntax:
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{
"value": "New",
"onclick": "CreateNewDoc()"
},
{
"value": "Open",
"onclick": "OpenDoc()"
},
{
"value": "Close",
"onclick": "CloseDoc()"
}
]
}
}
}

Example: json loading from buffer.
set($json, xml.json(\"{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } } }"\) ) // loading json from buffer
// do json stuff
alog($json) // print return value after load json from buffer, -1 is false, otherwise is successed.
xml.close($json) // close pointer

Example: Open json from buffer and write to file.
set($buf, \"{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language: ,http://www.arsa.ai","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}"\) // prepare buffer
set($json, xml.json($buf) ) // loading json from buffer
xml.save_json($json, "buf.json") // save to file
xml.close($json) // close pointer

◆ log()

void xml::log ( int  id,
const char *  option = "xml" 
)

debug and print xml or json buffer in log window

Parameters
idmap id.
optionfile type xml or json. default: xml

◆ logarray()

void xml::logarray ( int  array_id,
const char *  option = "xml" 
)

debug and print array of xml or json buffer in log window

Parameters
array_idid of array nodes
optionfile type xml or json. default: xml

◆ new()

int xml::new ( )

allocate empty xml document.

Returns
new empty xml document id, id >= 0 is successed, -1 is failed.
MUST call xml.close(id) after used for memory free.

◆ new_att()

bool xml::new_att ( int  node_id,
const char *  att_name,
const char *  att_value 
)

add attribute to xml node by attribute name and value.

Parameters
node_idid of xml node
att_namename of new attribute.
att_valuename of new value to attribute.
Returns
true is successed, false is failed

◆ new_node()

int xml::new_node ( int  root_id,
const char *  node_name,
const char *  value = "" 
)

add xml node with a name to root node and return new node as id.

Parameters
root_idid of root node
node_namenew name of xml node
valuea value of xml node that want to set.
Returns
new node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ next_child()

int xml::next_child ( int  node_id)

get next xml child in this node id.

Parameters
node_idid of xml node
Returns
child node id, id >= 0 is successed, -1 is failed.
MUST call xml.close_node() after used for memory free.

◆ save()

bool xml::save ( int  doc_id,
const char *  filename 
)

saving xml to file

Parameters
doc_idid of xml document, doc_id from functions: xml(), xml.json() and xml.new()
filenamename of file want to operator
Returns
true is successed, false is failed

◆ save_json()

bool xml::save_json ( int  doc_id,
const char *  filename 
)

saving json to file

Parameters
doc_idid of xml document, doc_id from functions: xml(), xml.json() and xml.new()
filenamename of file want to operator
Returns
true is successed, false is failed

◆ set_att_value()

bool xml::set_att_value ( int  node_id,
const char *  att_name,
const char *  att_value 
)

set attribute value to this node id.

Parameters
node_idid of xml node
att_namename of attribute that must exist!.
att_valuename of new value to this attribute.
Returns
true is successed, false is failed

◆ set_value()

bool xml::set_value ( int  node_id,
const char *  value 
)

set value to xml node.

Parameters
node_idid of xml node.
valuea value of xml node that want to set.
Returns
true is successed, false is failed

◆ tostring()

const char* xml::tostring ( int  doc_id)

saving xml to string buffer

Parameters
doc_idid of xml document, doc_id from functions: xml(), xml.json() and xml.new()
Returns
successed return string buffer in xml format, otherwise empty string.

◆ tostringjson()

const char* xml::tostringjson ( int  doc_id)

saving json to string buffer

Parameters
doc_idid of xml document, doc_id from functions: xml(), xml.json() and xml.new()
Returns
successed return string buffer in json format, otherwise empty string.

◆ value()

const char* xml::value ( int  node_id,
const char *  path 
)

get value of this path, use last path as element name

Parameters
node_idid of xml node
pathpath in xml or json that you want read value.
Returns
value of node, not empty strings is successed, otherwise is failed


Example 1: Load json from file with utf8.

{
set($str, ftostring("jsonarray.txt", "utf8"))
set($json, xml.json($str) )
set($id,0)
alog(xml.count($json, "array[$id]/faceRectangle"))
alog(xml.count($json, "array[$id]/faceRectangle/top"))
alog(xml.count($json, "array[$id]/faceId"))
alog(xml.count($json, "faceId"))
alog(xml.count($json, "array"))
alog(xml.value($json, "array[$id]/faceId"))
alog(xml.value($json, "array[$id]/faceRectangle/top"))
alog(xml.value($json, "array[$id]/faceRectangle/left"))
alog(xml.value($json, "array[$id]/faceRectangle/width"))
alog(xml.value($json, "array[$id]/faceRectangle/height"))
}function(init)


Result

1
1
1
1
3
49d55c17-e018-4a42-ba7b-8cbbdfae7c6f
131
177
162
162


JSon: jsonarray.txt

[
{
"faceId": "49d55c17-e018-4a42-ba7b-8cbbdfae7c6f",
"faceRectangle": {
"top": 131,
"left": 177,
"width": 162,
"height": 162
}
},
{
"faceId": "aaa",
"faceRectangle": {
"top": 50,
"left": 51,
"width": 52,
"height": 53
}
},
{
"faceId": "bbb",
"faceRectangle": {
"top": 45,
"left": 46,
"width": 47,
"height": 48
}
}
]


Example 2:

{
set($buf, \"{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language: ,http://www.arsa.ai","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}"\) // prepare buffer
set($json, xml.json($buf) ) // loading json from buffer
alog(xml.value($json, "glossary/GlossDiv/GlossList/GlossEntry/GlossDef/para"))
alog(xml.value($json, "glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso"))
set($ar, xml.array($json, "glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso") )
alog("array id: $ar")
xml.for($ar, proc( set($node,xml.get_array_element($ar,"null")) >> set($str_att,xml.get_att($node, "name")) >> set($str,xml.get_value($node))>> set($str_name,xml.get_name($node)) >> xml.close_node($node) >> alog("$str_name: value: $str, att: $str_att") ))
xml.save_json($json, "buf.json") // save to file
xml.close($json) // close pointer
}function(init)

Result

JSon:

{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language: ,http://www.arsa.ai",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}

The documentation for this class was generated from the following file: