Convert to read from file reader with decoder

Change-Id: I58845a612a6853658596154812f954efbbb6f733
This commit is contained in:
Greg Colburn
2015-11-26 22:00:23 -07:00
parent b698ad385d
commit c5c597ff92

112
dmarc.go
View File

@@ -3,6 +3,7 @@ package main
import ( import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"os"
) )
func main() { func main() {
@@ -70,96 +71,31 @@ func main() {
Record []Record `xml:"record"` Record []Record `xml:"record"`
} }
v := FeedbackReport{} xmlFile, err := os.Open("/Users/gcolburn/go/src/github.com/gc1code/dmarcparser/samples/google.xml") // For read access.
data := `
<?xml version="1.0" encoding="UTF-8" ?>
<feedback>
<report_metadata>
<org_name>google.com</org_name>
<email>noreply-dmarc-support@google.com</email>
<extra_contact_info>https://support.google.com/a/answer/2466580</extra_contact_info>
<report_id>11295852759969162400</report_id>
<date_range>
<begin>1448236800</begin>
<end>1448323199</end>
</date_range>
</report_metadata>
<policy_published>
<domain>colbs.net</domain>
<adkim>r</adkim>
<aspf>r</aspf>
<p>reject</p>
<sp>reject</sp>
<pct>100</pct>
</policy_published>
<record>
<row>
<source_ip>2607:f8b0:400c:c05::249</source_ip>
<count>1</count>
<policy_evaluated>
<disposition>none</disposition>
<dkim>pass</dkim>
<spf>fail</spf>
</policy_evaluated>
</row>
<identifiers>
<header_from>colbs.net</header_from>
</identifiers>
<auth_results>
<dkim>
<domain>google.com</domain>
<result>pass</result>
</dkim>
<dkim>
<domain>colbs.net</domain>
<result>pass</result>
</dkim>
<spf>
<domain>calendar-server.bounces.google.com</domain>
<result>pass</result>
</spf>
</auth_results>
</record>
<record>
<row>
<source_ip>2a00:1450:400c:c09::235</source_ip>
<count>1</count>
<policy_evaluated>
<disposition>none</disposition>
<dkim>pass</dkim>
<spf>pass</spf>
</policy_evaluated>
</row>
<identifiers>
<header_from>colbs.net</header_from>
</identifiers>
<auth_results>
<dkim>
<domain>colbs.net</domain>
<result>pass</result>
</dkim>
<spf>
<domain>colbs.net</domain>
<result>pass</result>
</spf>
</auth_results>
</record>
</feedback>
`
err := xml.Unmarshal([]byte(data), &v)
if err != nil { if err != nil {
fmt.Printf("error: %v", err) fmt.Printf("os error: %v", err)
return return
} }
fmt.Printf("XMLName: %#v\n", v) defer xmlFile.Close()
// fmt.Printf("org_name: %#q\n", v.ReportMetadata.OrgName)
// fmt.Printf("domain: %#q\n", v.PolicyPublished.Domain) decoder := xml.NewDecoder(xmlFile)
// fmt.Printf("Name: %q\n", v.Name) var inElement string
// fmt.Printf("Phone: %q\n", v.Phone) for {
// fmt.Printf("Email: %v\n", v.Email) t, _ := decoder.Token()
// fmt.Printf("Groups: %v\n", v.Groups) if t == nil {
// fmt.Printf("Address: %v\n", v.Address) break
}
switch se := t.(type) {
case xml.StartElement:
inElement = se.Name.Local
if inElement == "feedback" {
var f FeedbackReport
decoder.DecodeElement(&f, &se)
fmt.Printf("XMLName: %#v\n", f)
}
default:
}
}
} }