{"record":{"id":"a4bc2e28bf018b5f","repo":"mikefarah/yq","slug":"invalid-xml-encountered-chardata-v-outside-of","errorCode":null,"errorMessage":"invalid XML: Encountered chardata [%v] outside of XML node","messagePattern":"invalid XML: Encountered chardata \\[(.+?)\\] outside of XML node","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/decoder_xml.go","lineNumber":299,"sourceCode":"\t\t\t\tn:      &xmlNode{},\n\t\t\t\tlabel:  label,\n\t\t\t}\n\n\t\t\t// Extract attributes as children\n\t\t\tfor _, a := range se.Attr {\n\t\t\t\tif dec.prefs.KeepNamespace {\n\t\t\t\t\tif a.Name.Space != \"\" {\n\t\t\t\t\t\ta.Name.Local = a.Name.Space + \":\" + a.Name.Local\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telem.n.AddChild(dec.prefs.AttributePrefix+a.Name.Local, &xmlNode{Data: []string{a.Value}})\n\t\t\t}\n\t\tcase xml.CharData:\n\n\t\t\t// Extract XML data (if any)\n\t\t\tnewBit := trimNonGraphic(string(se))\n\t\t\tif !started && len(newBit) > 0 {\n\t\t\t\treturn fmt.Errorf(\"invalid XML: Encountered chardata [%v] outside of XML node\", newBit)\n\t\t\t}\n\n\t\t\tif len(newBit) > 0 {\n\t\t\t\telem.n.Data = append(elem.n.Data, newBit)\n\t\t\t\telem.state = \"chardata\"\n\t\t\t\tlog.Debugf(\"chardata [%v] for %v\", elem.n.Data, elem.label)\n\t\t\t}\n\t\tcase xml.EndElement:\n\t\t\tif elem == nil {\n\t\t\t\tlog.Debug(\"no element, probably bad xml\")\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tlog.Debugf(\"end element %v\", elem.label)\n\t\t\telem.state = \"finished\"\n\t\t\t// And add it to its parent list\n\t\t\tif elem.parent != nil {\n\t\t\t\telem.parent.n.AddChild(elem.label, elem.n)\n\t\t\t}","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/decoder_xml.go#L281-L317","documentation":"While streaming XML tokens, the decoder received character data (text) before any element had started — i.e. there is non-whitespace text at the top of the document outside the root node. Valid XML permits only whitespace and comments before the root element, so after trimming non-graphable characters any remaining leading chardata makes the document invalid.","triggerScenarios":"Thrown at pkg/yqlib/decoder_xml.go:299 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Remove stray text before the XML root element (the offending text is shown in the message)","Wrap loose text in a root element if it is meant to be data","Check for a truncated file that begins mid-text","Ensure the input is XML and not HTML/plain text"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}