Skip to content

Instantly share code, notes, and snippets.

@Matt3o12 Matt3o12/parser.go
Last active Jul 1, 2017

What would you like to do?
golang NonNamespaceString --
package rss
import "encoding/xml"
// NonNamespaceString is a special kind of string. When unmarshal'ed it
// will only unmarshal when there is no namespace.
// For more details, see:
type NonNamespaceString string
func (s *NonNamespaceString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if start.Name.Space != "" {
// We do not want a namespace, so we need to consume it
return nil
} else {
str := ""
d.DecodeElement(&str, &start)
*s = NonNamespaceString(str)
return nil
package rss
import (
func TestNonNamespaceString(t *testing.T) {
xmlFile, err := ioutil.ReadFile("with-namespace.xml")
if err != nil {
t.Fatalf("Error reading resource: %v", err)
c := struct {
XMLName xml.Name `xml:"rss"`
Channel struct {
Link NonNamespaceString `xml:"string"`
} `xml:"channel"`
xml.Unmarshal(xmlFile, &c)
expected := ""
if string(c.Channel.Link) == expected {
t.Errorf("Unexpected link: %v; wanted: %v", c.Channel.Link, expected)
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="">
<atom:link rel='hub' href=''/>
<atom:link rel="search" type="application/opensearchdescription+xml" href="" title="Example" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.