XML document, element, attribute, parser-result, and serialization helpers.


Core schemas


XMLParserResult


Parser model


parseStringToXML (source -- result)

Parses one accepted UTF-8 source into XMLParserResult.


saveXMLToString (xml -- string)

Serializes one XMLDocument to a formatted String.


Examples

Example: successful parse and stored counts

Parsing <root a="b"><child/></root> succeeds completely and stores one attribute and one child, all known during compilation.

"Xml"     use
"control" use

{} () {} [
  result: "<root a=\"b\"><child/></root>" parseStringToXML;
  @result.@success TRUE same printStack _:;
  @result.@finished TRUE same printStack _:;
  @result.@xml.@root.@attributes.size 1 same printStack _:;
  @result.@xml.@root.children.size 1 same printStack _:;
] "main" exportFunction

Expected Output During Compilation

TRUE
TRUE
TRUE
TRUE

Example: trailing input and rejected or discarded content

Trailing text leaves finished FALSE, a duplicate attribute and an apostrophe in element content make the parse fail, and whitespace-only content adds no child.

"Xml"     use
"control" use

{} () {} [
  trailing: "<root/>x" parseStringToXML;
  @trailing.@success TRUE same printStack _:;
  @trailing.@finished FALSE same printStack _:;

  duplicate: "<root a=\"1\" a=\"2\"/>" parseStringToXML;
  @duplicate.@success FALSE same printStack _:;

  spaces: "<root>   </root>" parseStringToXML;
  @spaces.@xml.@root.children.size 0 same printStack _:;

  apos: "<root>'</root>" parseStringToXML;
  @apos.@success FALSE same printStack _:;
] "main" exportFunction

Expected Output During Compilation

TRUE
TRUE
TRUE
TRUE
TRUE

Example: parse results at run time

At run time, the same document parses completely with one attribute and one child.

"String"  use
"Xml"     use
"control" use

{} Int32 {} [
  result: "<root a=\"b\"><child/></root>" parseStringToXML;
  ("success=" result.success LF
    "finished=" result.finished LF
    "attrs=" result.xml.root.attributes.size LF
    "children=" result.xml.root.children.size LF) printList
  0
] "main" exportFunction

Expected Output

success=TRUE
finished=TRUE
attrs=1
children=1

See also