[中文版本]
A lite non-standard xml parser in Chez Scheme.
- Parse XML into scheme tree structure
- Element will be convert into
((name . attr-alist) . child-list) - Toggle between space-preserving and space-ignoring modes.
- Declaration, xml head, and DOCTYPE declarations are silently skipped at any position.
- Supports XML comments inside open-tag bodies.
(Chez Scheme (v10.4.1) is required)
chez-xml is a single-file library,
./src/xml.sls is the source code.
Load it directly or compile to xml.so for faster loading.
By following steps, you can easily compile and run the test case.
git clone https://github.com/Yunoinsky/chez-xml # clone this repository
cd chez-xml
echo '(compile-file "src/xml.sls")' | scheme -q # optional: pre-compile
scheme --script ./test/test.ss
By copying xml.sls to the target project,
we can use chez-xml in other projects.
You can also choose to install it globally
as a pre-compiled library.
Here is one way to do this.
- Compile:
echo '(compile-file "src/xml.sls")' | scheme -q - Create lib directory: ~/Library/chezscheme/.
- Copy
xml.soto this directory. - Append
/Library/chezscheme/ to ~CHEZSCHEMELIBDIRS. - Then,
(import (xml))works in any project.
Take an excerpt from the API of Raysan’s outstanding project - raylib as an example.
<?xml version="1.0" encoding="Windows-1252" ?>
<!-- ./test/test_raylibapi.xml -->
<!-- https://raw.githubusercontent.com/raysan5/raylib/refs/heads/master/tools/rlparser/output/raylib_api.xml -->
<raylibAPI>
<Defines count="4">
<Define name="PI" type="FLOAT" value="3.14159265358979323846" desc="" />
<Define name="DEG2RAD" type="FLOAT_MATH" value="(PI/180.0f)" desc="" />
<Define name="RED" type="COLOR" value="CLITERAL(Color){ 200, 200, 200, 255 }" desc="Light Gray" />
<Define name="BLUE" type="COLOR" value="CLITERAL(Color){ 0, 121, 241, 255 }" desc="Blue" />
</Defines>
<Structs count="4">
<Struct name="Vector2" fieldCount="2" desc="Vector2, 2 components">
<Field type="float" name="x" desc="Vector x component" />
<Field type="float" name="y" desc="Vector y component" />
</Struct>
<Struct name="Vector3" fieldCount="3" desc="Vector3, 3 components">
<Field type="float" name="x" desc="Vector x component" />
<Field type="float" name="y" desc="Vector y component" />
<Field type="float" name="z" desc="Vector z component" />
</Struct>
<Struct name="Color" fieldCount="4" desc="Color, 4 components, R8G8B8A8 (32bit)">
<Field type="unsigned char" name="r" desc="Color red value" />
<Field type="unsigned char" name="g" desc="Color green value" />
<Field type="unsigned char" name="b" desc="Color blue value" />
<Field type="unsigned char" name="a" desc="Color alpha value" />
</Struct>
<Struct name="Rectangle" fieldCount="4" desc="Rectangle, 4 components">
<Field type="float" name="x" desc="Rectangle top-left corner position x" />
<Field type="float" name="y" desc="Rectangle top-left corner position y" />
<Field type="float" name="width" desc="Rectangle width" />
<Field type="float" name="height" desc="Rectangle height" />
</Struct>
</Structs>
<Functions count="2">
<Function name="InitWindow" retType="void" paramCount="3" desc="Initialize window and OpenGL context">
<Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" />
<Param type="const char *" name="title" desc="" />
</Function>
<Function name="IsWindowReady" retType="bool" paramCount="0" desc="Check if window has been initialized successfully">
</Function>
</Functions>
</raylibAPI> (load "./src/xml.sls") ;; this line can be commented
;; out if installed globally
(import (xml))- (xml-load text-port preserve-blank)
-
Load xml-data from a text-port, return a scheme tree.
If preserve-blank is
#t, all of the blank chars will be preserved and parsed as strings, or they will be ignored.
(define fp (open-input-file "./test/test_raylibapi.xml"))
(define data (xml-load fp #f))
(pretty-print data)- (xml-get-name node)
- Get the name of xml node.
- (xml-get-attrs node)
- Get the attrs alist of xml node.
- (xml-get-children node)
- Get the children’s list of xml node.
(define funcs-data (caddr (xml-get-children data)))
(pretty-print (xml-get-name funcs-data))
(pretty-print (xml-get-attrs funcs-data))
(define func1-data (car (xml-get-children funcs-data)))
(pretty-print (xml-get-name func1-data))
(pretty-print (xml-get-attrs func1-data))
(pretty-print (xml-get-children func1-data))