MCPcopy Create free account
hub / github.com/antchfx/xmlquery / ParseWithOptions

Function ParseWithOptions

parse.go:40–93  ·  view source on GitHub ↗

ParseWithOptions is like parse, but with custom options

(r io.Reader, options ParserOptions)

Source from the content-addressed store, hash-verified

38
39// ParseWithOptions is like parse, but with custom options
40func ParseWithOptions(r io.Reader, options ParserOptions) (*Node, error) {
41 var lineStarts []int
42 // If line numbers are requested, read all data for position tracking
43 if options.WithLineNumbers {
44 var err error
45 var data []byte
46 data, err = io.ReadAll(r)
47 if err != nil {
48 return nil, err
49 }
50 r = bytes.NewReader(data)
51
52 // Pre-calculate line starts
53 lineStarts = []int{0}
54 for i, b := range data {
55 if b == '\n' {
56 lineStarts = append(lineStarts, i+1)
57 }
58 }
59 }
60
61 p := createParser(r)
62 if options.WithLineNumbers {
63 p.lineStarts = lineStarts
64 }
65 options.apply(p)
66
67 var err error
68 for err == nil {
69 _, err = p.parse()
70 }
71
72 if err == io.EOF {
73 // additional check for validity
74 // according to: https://www.w3.org/TR/xml
75 // the document MUST contain at least ONE element
76 valid := false
77 for doc := p.doc; doc != nil; doc = doc.NextSibling {
78 for node := doc.FirstChild; node != nil; node = node.NextSibling {
79 if node.Type == ElementNode {
80 valid = true
81 break
82 }
83 }
84 }
85 if !valid {
86 return nil, fmt.Errorf("xmlquery: invalid XML document")
87 }
88
89 return p.doc, nil
90 }
91
92 return nil, err
93}
94
95type parser struct {
96 decoder *xml.Decoder

Callers 2

TestLineNumbersFunction · 0.85
ParseFunction · 0.85

Calls 3

createParserFunction · 0.85
parseMethod · 0.80
applyMethod · 0.45

Tested by 1

TestLineNumbersFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…