2 xml-to-hash
(xml-file-to-hash (-> path-string? (listof (cons/c string? (or/c 'v 'a))) hash?))
Load xml to hash map.
Specify a hierachy string list to get value or attribute: ’("list.a.element" "list.a.attribute")
Result is a string.
Node value vs Attribute, If node value is null: ></ or />, node value is "", if attribute not exist, value is not exist in result map too.
Every parent node has a "xxx’s count" value, tells this level’s node’s count.
Use sequence appended after node name to get value or attribute
2.1 Basic Usage
<empty attr1="a1" attr2="a2">v</empty>
(let ([xml_hash (xml-file-to-hash empty2_xml_file '( "empty" "empty.attr1" "empty.attr2" ) )]) (printf "xml hash has ~a keys.\n" (hash-count xml_hash)) (printf "empty's count: ~a\n" (hash-ref xml_hash "empty's count")) (printf "empty's value: ~a\n" (hash-ref xml_hash "empty1")) (printf "empty.attr1: ~a\n" (hash-ref xml_hash "empty1.attr11")) (printf "empty.attr2: ~a\n" (hash-ref xml_hash "empty1.attr21"))) xml hash has 4 keys. empty's count: 1 empty's value: v empty.attr1: a1 empty.attr2: a2
<empty attr1="a1" attr2="a2"/>
2.2 Hierachy
XML is a hierachy structure text format. So, xml-to-hash refect the hierachy information. You access a node’s attribute or content, you should give all the ancester’s name in order.
<level1> <level2> <level3 attr="a3"> <level4>Hello Xml!</level4> </level3> </level2> </level1>
(let ([xml_hash (xml-file-to-hash "hierachy.xml" '( "level1.level2.level3.attr" "level1.level2.level3.level4" ))]) (printf "level1.level2.level3.attr: [~a]\n" (hash-ref xml_hash "level11.level21.level31.attr1")) (printf "level1.level2.level3.level4: [~a]\n" (hash-ref xml_hash "level11.level21.level31.level41")) ) level1.level2.level3.attr: [a3] level1.level2.level3.level4: [Hello Xml!]
2.3 Default Attribute
If some node has no some attribute, then the value is not exists.
<list> <child attr="a1">c1</child> <child>c2</child> <child attr="a3">c3</child> </list>
(let ([xml_hash (xml-file-to-hash "default.xml" '( "list.child.attr" ))]) (printf "list1.child1.attr1: [~a]\n" (hash-ref xml_hash "list1.child1.attr1")) (printf "list1.child1.attr2: [~a]\n" (hash-ref xml_hash "list1.child1.attr2" "")) ) list1.child1.attr1: [a1] list1.child1.attr2: []
2.4 Entity Convert
There are two kinds of entity:
1. numeric character reference
&#xhhhh; or &#nnnn;
where the x must be lowercase in XML documents, hhhh is the code point in hexadecimal form, and nnnn is the code point in decimal form.
2. 5 predefined name
&name;
&[&], <[<], >[>], '['], and "["].
when reading from xml, attributes and values will be convert from entity to specific character.
when writing to xml, in attributes and values, 5 special chars will be converted to entity string.