Hi,
XML is as below.
<?xml version="1.0" encoding="Shift_JIS"?> <Filling_data> <destination_ID>05</destination_ID> <sender_ID>01</sender_ID> <type>PGFILE </type> <remaining_capacity>00000</remaining_capacity> </Filling_data>
Here i want to validate each value in tag, am trying to validate XML input based on regexp. My validate function is failing to validate the xml tag. Even if i enter <destination_ID>09</destination_ID> or <destination_ID>05</destination_ID>, it always prints not Destination_ID, can you please let me what is the issue.
package main import ( "encoding/xml" "fmt" "golang.org/x/text/encoding/japanese" "golang.org/x/text/transform" "gopkg.in/validator.v2" "io" "os" )
type PGS_filing_data struct { Destination_ID string `xml:"destination_ID" Motu:"regexp=(01|05)"` Sender_ID string `xml:"sender_ID" sender_ID:"regexp=(01|05)"` Type_ID string `xml:"type" type_ID:"regexp=(PGFILE△△|PGFILER△|PGALL△△△)"` Remaining_capacity string `xml:"remaining_capacity" remaining_capacity:"regexp=([0-9]{3}[0-5][0-9]|△△△△△)"` }
// DoSampleImpl ... func DoSampleImpl() {
// Read XML File. Note! This file is SJIS encoding. fp, err := os.Open(cs) if err != nil { fmt.Println("Error opening file:", err) return } defer fp.Close() fp.Seek(0, 0) // bind xml string decoder := xml.NewDecoder(fp) decoder.CharsetReader = func(charset string, r io.Reader) (io.Reader, error) { return transform.NewReader(r, japanese.ShiftJIS.NewDecoder()), nil } data := new(PGS_filing_data) err = decoder.Decode(&data) if err != nil { fmt.Println(err.Error()) rt.Information = err.Error() } fmt.Println("[!!!!", data, "]")
// show result fmt.Println("[", data.Destination_ID, "]") t1 := validator.NewValidator() t1.SetTag("Motu") if err := t1.Validate(data.Destination_ID); err != nil { fmt.Println("!", data.Destination_ID, " is not Destination_ID") } else { fmt.Println(" -", data.Destination_ID, "is valid Destination_ID") } return }