Documentation ¶
Overview ¶
Package parser provides implementations of xml parsers.
It's main purpose is to provide xml parser which is suitable for generating xtree structure used in xml comparison with xdiff algorithm.
There are two parser implementations available:
- The standard implementation using encoding/xml library,
- and the custom in-place parser which is much faster but less robust.
Use standard parser if you need robust and well tested parser and you don't care about it's performance. Use custom in-place parser if you need fast performance but can sacrifice robustness. In each case test it first on your own xml data.
There are three functionalities that require xdiff xtree parser:
- Assign signatures and hashes to the nodes,
- Parse directories as nodes,
- Parse attributes as nodes,
- Optimizing execution speed and resource usage, for more info see https://github.com/golang/go/issues/21823
Example API usage:
p := parser.New() // to parse from reader xtree, err := p.ParseReader(openedFileReader) // to parse from bytes xtree, err := p.ParseBytes(filebytes) // to parse from filepath xtree, err := p.ParseFile("filename.xml") // to parse from dirpath xtree, err := p.ParseDir("dirname")
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnexpectedEnd = errors.New("parser: unexpected end of data")
ErrUnexpectedEnd represents attempted read beyond available data.
Functions ¶
This section is empty.
Types ¶
type Standard ¶
type Standard struct { // Handler for non-xml files encountered during directory traversal. NonXMLHandler func(f *os.File, fi os.FileInfo) (*xtree.Node, error) }
Standard is a standard parser using encoding/xml sax parser as engine.
func (*Standard) ParseBytes ¶
ParseBytes returns reference to the document node parsed from provided bytes.
func (*Standard) ParseDir ¶
ParseDir returns reference to the directory node got by parsing provided dirpath. Each directory is parent to the child xml documents and documents have xtree structure got by parsing xml.
RootDir |- Child1.xml | `- rootElement | |- childElement | `- childElement
`- Child2.xml `- rootElement
Use NonXMLHandler flag on the parser to configure behavior for handling non-xml files found in the directories.
type XDiff ¶
type XDiff struct { // Whether or not to validate closing tags. ValidateClosingTag bool // Handler for non-xml files encountered during directory traversal. NonXMLHandler func(f *os.File, fi os.FileInfo) (*xtree.Node, error) // Set document node name to filename when parsing xml by filename. SetDocumentFilename bool // contains filtered or unexported fields }
XDiff is a custom XML parser able to parse xml files to generate structures needed for doing xtree comparison in XDiff algorithm.
Besides standard xml parsing, parsed xtree is also able to include nodes formed by the directory structure in which xml files reside. This is useful for comparing xml data which is spread across different directories and files.
Parser is doing only basic transformation on the xml document to preserve documents as is for better comparison.
func (*XDiff) ParseBytes ¶
ParseBytes returns reference to the document node parsed from provided bytes. For performance reasons returned structure will reuse byte array from the provided slice. Do not manipulate with it until you are done using the parsed structure. Pass the copy of the slice if you want to maintain ownership of the bytes.
func (*XDiff) ParseDir ¶
ParseDir returns reference to the directory node got by parsing provided dirpath. Each directory is parent to the child xml documents and documents have xtree structure got by parsing xml.
RootDir |- Child1.xml | `- rootElement | |- childElement | `- childElement
`- Child2.xml `- rootElement
Use NonXMLHandler flag on the parser to configure behavior for handling non-xml files found in the directories.