mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-13 09:22:22 -07:00
b5c828fe4e
A from-scratch XML implementation, loosely modeled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816). Features: - Supports enough of the XML 1.0/1.1 spec to handle the 99.9% of XML documents in common current usage. - Simple to understand and use. Small. Caveats: - We do NOT support HTML in this package, as that may or may not be valid XML. If it works, great. If it doesn't, that's not considered a bug. - We do NOT support UTF-16. If you have a UTF-16 XML file, please convert it to UTF-8 first. Also, our condolences. - <[!ELEMENT and <[!ATTLIST are not supported, and will be either ignored or return an error depending on the parser options. TODO: - Optional CDATA unboxing. - Optional `>`, ` `, ` ` and other escape substitution in tag bodies. - Test suite MAYBE: - XML writer? - Serialize/deserialize Odin types?
73 lines
1.5 KiB
Odin
73 lines
1.5 KiB
Odin
package xml
|
|
/*
|
|
An XML 1.0 / 1.1 parser
|
|
|
|
Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
|
|
Made available under Odin's BSD-3 license.
|
|
|
|
A from-scratch XML implementation, loosely modeled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816).
|
|
|
|
List of contributors:
|
|
Jeroen van Rijn: Initial implementation.
|
|
*/
|
|
import "core:fmt"
|
|
|
|
/*
|
|
Just for debug purposes.
|
|
*/
|
|
print :: proc(doc: ^Document) {
|
|
assert(doc != nil)
|
|
|
|
using fmt
|
|
println("[XML Prolog]")
|
|
|
|
for attr in doc.prolog {
|
|
printf("\t%v: %v\n", attr.key, attr.val)
|
|
}
|
|
|
|
printf("[Encoding] %v\n", doc.encoding)
|
|
printf("[DOCTYPE] %v\n", doc.doctype.ident)
|
|
|
|
if len(doc.doctype.rest) > 0 {
|
|
printf("\t%v\n", doc.doctype.rest)
|
|
}
|
|
|
|
if doc.root != nil {
|
|
println(" --- ")
|
|
print_element(0, doc.root)
|
|
println(" --- ")
|
|
}
|
|
}
|
|
|
|
print_element :: proc(indent: int, element: ^Element) {
|
|
if element == nil { return }
|
|
using fmt
|
|
|
|
tab :: proc(indent: int) {
|
|
tabs := "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
|
|
|
|
i := max(0, min(indent, len(tabs)))
|
|
printf("%v", tabs[:i])
|
|
}
|
|
|
|
tab(indent)
|
|
|
|
if element.kind == .Element {
|
|
printf("<%v>\n", element.ident)
|
|
if len(element.value) > 0 {
|
|
tab(indent + 1)
|
|
printf("[Value] %v\n", element.value)
|
|
}
|
|
|
|
for attr in element.attribs {
|
|
tab(indent + 1)
|
|
printf("[Attr] %v: %v\n", attr.key, attr.val)
|
|
}
|
|
|
|
for child in element.children {
|
|
print_element(indent + 1, child)
|
|
}
|
|
} else if element.kind == .Comment {
|
|
printf("[COMMENT] %v\n", element.value)
|
|
}
|
|
} |