From b90c3e0dcdd0b52051eb7ec4930a3768efd9c6b9 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Mon, 13 Mar 2017 21:10:19 -0400 Subject: [PATCH] Ignore whitespace character data xml Tokens --- ofx.go | 29 +++++++++++++++++++++++------ profile.go | 8 ++++---- signup.go | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/ofx.go b/ofx.go index be75ed7..a1871fe 100644 --- a/ofx.go +++ b/ofx.go @@ -239,8 +239,25 @@ func (or *Response) readSGMLHeaders(r *bufio.Reader) error { return nil } +func nextNonWhitespaceToken(decoder *xml.Decoder) (xml.Token, error) { + for { + tok, err := decoder.Token() + if err != nil { + return nil, err + } else if chars, ok := tok.(xml.CharData); ok { + strippedBytes := bytes.TrimSpace(chars) + if len(strippedBytes) != 0 { + return tok, nil + } + } else { + return tok, nil + } + } +} + func (or *Response) readXMLHeaders(decoder *xml.Decoder) error { - tok, err := decoder.Token() + var tok xml.Token + tok, err := nextNonWhitespaceToken(decoder) if err != nil { return err } else if xmlElem, ok := tok.(xml.ProcInst); !ok || xmlElem.Target != "xml" { @@ -248,7 +265,7 @@ func (or *Response) readXMLHeaders(decoder *xml.Decoder) error { } // parse the OFX header - tok, err = decoder.Token() + tok, err = nextNonWhitespaceToken(decoder) if err != nil { return err } else if ofxElem, ok := tok.(xml.ProcInst); ok && ofxElem.Target == "OFX" { @@ -330,7 +347,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error { } } - tok, err := decoder.Token() + tok, err := nextNonWhitespaceToken(decoder) if err != nil { return err } else if ofxStart, ok := tok.(xml.StartElement); !ok || ofxStart.Name.Local != "OFX" { @@ -338,7 +355,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error { } // Unmarshal the signon message - tok, err = decoder.Token() + tok, err = nextNonWhitespaceToken(decoder) if err != nil { return err } else if signonStart, ok := tok.(xml.StartElement); ok && signonStart.Name.Local == "SIGNONMSGSRSV1" { @@ -349,7 +366,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error { return errors.New("Missing opening SIGNONMSGSRSV1 xml element") } - tok, err = decoder.Token() + tok, err = nextNonWhitespaceToken(decoder) if err != nil { return err } else if signonEnd, ok := tok.(xml.EndElement); !ok || signonEnd.Name.Local != "SIGNONMSGSRSV1" { @@ -360,7 +377,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error { } for { - tok, err = decoder.Token() + tok, err = nextNonWhitespaceToken(decoder) if err != nil { return err } else if ofxEnd, ok := tok.(xml.EndElement); ok && ofxEnd.Name.Local == "OFX" { diff --git a/profile.go b/profile.go index f572f24..6cdaac2 100644 --- a/profile.go +++ b/profile.go @@ -63,7 +63,7 @@ type MessageSetList []MessageSet func (msl *MessageSetList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { for { var msgset MessageSet - tok, err := d.Token() + tok, err := nextNonWhitespaceToken(d) if err != nil { return err } else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local { @@ -71,7 +71,7 @@ func (msl *MessageSetList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) return nil } else if _, ok := tok.(xml.StartElement); ok { // Found starting tag for . Get the next one (xxxMSGSETVn) and decode that struct - tok, err := d.Token() + tok, err := nextNonWhitespaceToken(d) if err != nil { return err } else if versionStart, ok := tok.(xml.StartElement); ok { @@ -83,7 +83,7 @@ func (msl *MessageSetList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) } // Eat ending tags for - tok, err = d.Token() + tok, err = nextNonWhitespaceToken(d) if err != nil { return err } else if _, ok := tok.(xml.EndElement); !ok { @@ -129,7 +129,7 @@ func (pr ProfileResponse) Valid() (bool, error) { func DecodeProfileMessageSet(d *xml.Decoder, start xml.StartElement) ([]Message, error) { var msgs []Message for { - tok, err := d.Token() + tok, err := nextNonWhitespaceToken(d) if err != nil { return nil, err } else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local { diff --git a/signup.go b/signup.go index 0463843..f56798b 100644 --- a/signup.go +++ b/signup.go @@ -146,7 +146,7 @@ func (air AcctInfoResponse) Valid() (bool, error) { func DecodeSignupMessageSet(d *xml.Decoder, start xml.StartElement) ([]Message, error) { var msgs []Message for { - tok, err := d.Token() + tok, err := nextNonWhitespaceToken(d) if err != nil { return nil, err } else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local {