v/vlib/encoding/xml
2024-04-18 02:44:31 +03:00
..
test xml: update entity parse test (#21190) 2024-04-05 17:31:32 +03:00
encoding.v encoding.xml: make functions public, add documentation, tests, fix attribute parsing for self-closing tags (#19901) 2023-11-16 20:13:36 +02:00
encoding_test.v encoding.xml: make functions public, add documentation, tests, fix attribute parsing for self-closing tags (#19901) 2023-11-16 20:13:36 +02:00
entity.v breaking,checker: disallow initializing private struct fields outside structs module (#21183) 2024-04-12 13:53:02 +03:00
entity_test.v vlib: add an encoding.xml module with parser, validation, entity encoding, unit tests (#19708) 2023-11-06 15:14:30 +02:00
parser.v vlib: refactor empty string checks to use s == '' or s != '', instead of s.len == 0 (#21300) 2024-04-18 02:44:31 +03:00
parser_test.v all: unwrap const() blocks 2023-11-25 10:02:51 +03:00
query.v encoding.xml: make functions public, add documentation, tests, fix attribute parsing for self-closing tags (#19901) 2023-11-16 20:13:36 +02:00
query_test.v all: unwrap const() blocks 2023-11-25 10:02:51 +03:00
reader_util.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
README.md encoding.xml: make functions public, add documentation, tests, fix attribute parsing for self-closing tags (#19901) 2023-11-16 20:13:36 +02:00
types.v breaking,checker: disallow initializing private struct fields outside structs module (#21183) 2024-04-12 13:53:02 +03:00
validation.v vlib: refactor empty string checks to use s == '' or s != '', instead of s.len == 0 (#21300) 2024-04-18 02:44:31 +03:00

Description

xml is a module to parse XML documents into a tree structure. It also supports validation of XML documents against a DTD.

Note that this is not a streaming XML parser. It reads the entire document into memory and then parses it. This is not a problem for small documents, but it might be a problem for extremely large documents (several hundred megabytes or more).

The public function parse_single_node can be used to parse a single node from an implementation of io.Reader, which can help parse large XML documents on an element-by-element basis. Sample usage is provided in the parser_test.v file.

Usage

Parsing XML Files

There are three different ways to parse an XML Document:

  1. Pass the entire XML document as a string to XMLDocument.from_string.
  2. Specify a file path to XMLDocument.from_file.
  3. Use a source that implements io.Reader and pass it to XMLDocument.from_reader.
import encoding.xml

//...
doc := xml.XMLDocument.from_file('test/sample.xml')!

Validating XML Documents

Simply call validate on the parsed XML document.

Querying

Check the get_element... methods defined on the XMLDocument struct.

Escaping and Un-escaping XML Entities

When the validate method is called, the XML document is parsed and all text nodes are un-escaped. This means that the text nodes will contain the actual text and not the escaped version of the text.

When the XML document is serialized (using str or pretty_str), all text nodes are escaped.

The escaping and un-escaping can also be done manually using the escape_text and unescape_text methods.