diff --git a/readme.md b/readme.md index 64dfe72..78863a5 100644 --- a/readme.md +++ b/readme.md @@ -42,6 +42,7 @@ For over a year I've wanted to refactor the XML-to-map[string]interface{} decode

Notices

+ 2020.12.14: v2.5 - add XmlCheckIsValid toggle to force checking that the encoded XML is valid 2020.12.14: v2.4 - add XMLEscapeCharsDecoder to preserve XML escaped characters in Map values 2020.10.28: v2.3 - add TrimWhiteSpace option 2020.05.01: v2.2 - optimize map to XML encoding for large XML docs. diff --git a/xml.go b/xml.go index a2dbbf1..a551186 100644 --- a/xml.go +++ b/xml.go @@ -633,6 +633,20 @@ func XmlDefaultEmptyElemSyntax() { useGoXmlEmptyElemSyntax = false } +// ------- issue #88 ---------- +// xmlCheckIsValid set switch to force decoding the encoded XML to +// see if it is valid XML. +var xmlCheckIsValid bool + +// XmlCheckIsValid forces the encoded XML to be checked for validity. +func XmlCheckIsValid(b ...bool) { + if len(b) == 1 { + xmlCheckIsValid = b[0] + return + } + xmlCheckIsValid = !xmlCheckIsValid +} + // Encode a Map as XML. The companion of NewMapXml(). // The following rules apply. // - The key label "#text" is treated as the value for a simple element with attributes. @@ -679,6 +693,11 @@ func (mv Map) Xml(rootTag ...string) ([]byte, error) { err = marshalMapToXmlIndent(false, b, DefaultRootTag, m, p) } done: + if xmlCheckIsValid { + if _, err = NewMapXml(b.Bytes()); err != nil { + return nil, err + } + } return b.Bytes(), err } @@ -917,6 +936,11 @@ func (mv Map) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, error } else { err = marshalMapToXmlIndent(true, b, DefaultRootTag, m, p) } + if xmlCheckIsValid { + if _, err = NewMapXml(b.Bytes()); err != nil { + return nil, err + } + } return b.Bytes(), err } diff --git a/xmlseq.go b/xmlseq.go index 559b028..6427d0f 100644 --- a/xmlseq.go +++ b/xmlseq.go @@ -447,6 +447,11 @@ func (mv MapSeq) Xml(rootTag ...string) ([]byte, error) { err = mapToXmlSeqIndent(false, s, DefaultRootTag, m, p) } done: + if xmlCheckIsValid { + if _, err = NewMapXml([]byte(*s)); err != nil { + return nil, err + } + } return []byte(*s), err } @@ -535,6 +540,11 @@ func (mv MapSeq) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, er } else { err = mapToXmlSeqIndent(true, s, DefaultRootTag, m, p) } + if xmlCheckIsValid { + if _, err = NewMapXml([]byte(*s)); err != nil { + return nil, err + } + } return []byte(*s), err }