Skip to content

Commit

Permalink
Fix Issue #88
Browse files Browse the repository at this point in the history
Add XmlCheckIsValid to address Issue #88.
  • Loading branch information
clbanning committed Feb 1, 2021
1 parent 6dbd197 commit 3c6efab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ For over a year I've wanted to refactor the XML-to-map[string]interface{} decode

<h4>Notices</h4>

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.
Expand Down
24 changes: 24 additions & 0 deletions xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions xmlseq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3c6efab

Please sign in to comment.