package xmlparser
import "github.com/edragoev1/pdfjet/v9/src/xmlparser"
Package xmlparser reads XML into elements: their names, their namespaces, their attributes in the order they are written, and their text. It is the one parser of the library and of PDFjet Pro: the library reads SVG images with it, in the four ports alike, and PDFjet Pro the XML of electronic invoices, the Cross Industry Invoice of ZUGFeRD and Factur-X. It reads what such a document is made of and no more, and what it leaves out is what makes XML dangerous to read:
- A document type declaration, a DOCTYPE, is refused by Parse and ParseReader, as an invoice has none, and skipped by ParseSkippingDoctype and ParseReaderSkippingDoctype, as the SVG files of the drawing programs have one. Its entities are not read either way: an entity that names a file or a URL reads what it should not, and the ones that stand for each other grow a short document into gigabytes.
- The entities are the five of XML, <, >, &, " and ', and the numeric ones, such as   and  . Any other is an error, the ones a DOCTYPE declares among them.
- The elements nest MaxDepth levels at most, and the reading does not recurse, so no document overflows the stack.
- The document is 20 MB at most, and has MaxElements elements at most.
- The namespaces are looked up in the elements that declare them, from the innermost out, and not copied into each element, so that no number of declarations makes the reading slower than the length of the document times its depth.
- Nothing is fetched, and no schema is read.
What is read is XML, and what is not is an error: a character that XML does not have, such as U+0000 or U+FFFF, written or as an entity; an attribute written twice, or not after a space; and a prefix that no element declares. A line break is a line feed, as XML reads a carriage return and the line feed after it, and the tabs and line breaks in the value of an attribute are spaces, as XML normalizes them.
The document is UTF-8, or UTF-16 when it starts with a byte order mark; another encoding in the declaration is an error.
The document is read as characters, as the Java port reads it. The bytes that are not valid UTF-8 are the character U+FFFD, one for each part of a sequence that is as much of a character as it is valid for, which is what the Unicode Standard recommends and the decoders of the JDK, of .NET and of Swift do. A code unit of UTF-16 that is a surrogate without the other one of its pair is U+FFFD as well.
Index
- Constants
- Variables
- func DecodeUTF16(bytes []byte, littleEndian bool) []rune
- func Trim(text string) string
- type XMLAttribute
-
type XMLNode
- func Parse(bytes []byte) (*XMLNode, error)
- func ParseReader(in io.Reader) (*XMLNode, error)
- func ParseReaderSkippingDoctype(in io.Reader) (*XMLNode, error)
- func ParseSkippingDoctype(bytes []byte) (*XMLNode, error)
- func (node *XMLNode) Attribute(attributeName string) (string, bool)
- func (node *XMLNode) Attributes() []XMLAttribute
- func (node *XMLNode) Children() []*XMLNode
- func (node *XMLNode) Find(path string) *XMLNode
- func (node *XMLNode) FindAll(path string) []*XMLNode
- func (node *XMLNode) LocalName() string
- func (node *XMLNode) Name() string
- func (node *XMLNode) Namespace() string
- func (node *XMLNode) Text() string
- func (node *XMLNode) Value(path string) (string, bool)
Constants
const MaxDepth = 256
MaxDepth is how deep the elements of a document may nest.
const MaxElements = 1000000
MaxElements is how many elements a document may have, a million: an invoice of the 20 MB has fewer than half as many, and an SVG image has its points in the attributes of its paths, not in elements. It keeps a document of tiny elements from taking hundreds of megabytes (the review of 9 October 2026).
const MaxSize = 20 << 20
MaxSize is how many bytes a document may be, 20 MB: an invoice of some fifteen thousand lines, or an SVG image of millions of points.
Variables
var ErrTooLarge = errors.New("The document is more than 20 MB.")
ErrTooLarge is the error of a document of more than MaxSize bytes.
Functions
func DecodeUTF16
func DecodeUTF16(bytes []byte, littleEndian bool) []rune
DecodeUTF16 returns the characters of the bytes of UTF-16, big-endian or little-endian, without a byte order mark: a code unit that is a surrogate without the other one of its pair, or a byte without the one after it, is U+FFFD, as the reader of the document reads them.
func Trim
func Trim(text string) string
Trim returns the text without the characters up to and including the space at its ends, as Java's String.trim does: the space and the control characters of a text, and not the other whitespace of Unicode.
Types
type XMLAttribute
type XMLAttribute struct { Name string Value string }
XMLAttribute is an attribute of an element: its name as the document writes it, with its prefix when it has one, such as xlink:href, and its value.
type XMLNode
type XMLNode struct { // contains filtered or unexported fields }
XMLNode is an element of an XML document that Parse read: its name, its attributes, its text and the elements in it.
The elements are found by a path of the names without their prefix, such as "SupplyChainTradeTransaction/ApplicableHeaderTradeAgreement/BuyerReference", because the prefixes of an invoice are its own: what one writes as ram:ID another writes as a:ID. A * matches an element of any name.
func Parse
func Parse(bytes []byte) (*XMLNode, error)
func ParseReader
func ParseReader(in io.Reader) (*XMLNode, error)
ParseReader reads the document of the reader as Parse does and returns its root element. The reader is read to its end, or to the MaxSize bytes a document may be, and is not closed.
func ParseReaderSkippingDoctype
func ParseReaderSkippingDoctype(in io.Reader) (*XMLNode, error)
ParseReaderSkippingDoctype reads the document of the reader as ParseSkippingDoctype does.
func ParseSkippingDoctype
func ParseSkippingDoctype(bytes []byte) (*XMLNode, error)
ParseSkippingDoctype reads the document as Parse does, and skips a DOCTYPE, which the SVG files of the drawing programs have. Its entities are not read, and a reference to one is an error. A prefix that no element declares is read as no namespace, as SVG files copied from web pages have xlink:href without xmlns:xlink, rather than an error.
func (*XMLNode) Attribute
func (node *XMLNode) Attribute(attributeName string) (string, bool)
Attribute returns the value of the attribute of that name, by its name with its prefix, such as unitCode or ram:unitCode, and whether the element has such an attribute.
func (*XMLNode) Attributes
func (node *XMLNode) Attributes() []XMLAttribute
Attributes returns the attributes of the element, by their names as the document writes them, in the order it writes them.
func (*XMLNode) Children
func (node *XMLNode) Children() []*XMLNode
Children returns the elements in this one, in their order.
func (*XMLNode) Find
func (node *XMLNode) Find(path string) *XMLNode
Find returns the first element at the path under this one, or nil.
func (*XMLNode) FindAll
func (node *XMLNode) FindAll(path string) []*XMLNode
FindAll returns the elements at the path under this one, none when there are none: the names of the path are the names without their prefix, and a * matches an element of any name. The path is such as "IncludedSupplyChainTradeLineItem/SpecifiedTradeProduct/Name".
func (*XMLNode) LocalName
func (node *XMLNode) LocalName() string
LocalName returns the name of the element without its prefix, such as ID.
func (*XMLNode) Name
func (node *XMLNode) Name() string
Name returns the name of the element as the document writes it, with its prefix when it has one, such as ram:ID.
func (*XMLNode) Namespace
func (node *XMLNode) Namespace() string
Namespace returns the namespace of the element, the URI its prefix stands for, or an empty string when it is in no namespace.
func (*XMLNode) Text
func (node *XMLNode) Text() string
Text returns the text of the element, the characters in it that are not in the elements it holds, as they are written.
func (*XMLNode) Value
func (node *XMLNode) Value(path string) (string, bool)
Value returns the text of the first element at the path under this one, without the spaces and line breaks at its ends, and whether the path has an element.