kadalamittai
kadalamittai

Reputation: 2166

Xml Unmarshal failing

i am trying to Unmarshal simple Xml Schema to Struct. appears to be failing if i have  found in any of my Xml Node value.

Reading an Xml file leads to runtime error. Reading an Xml string leads to missing all other

Sample : http://play.golang.org/p/waNn_1NpD1

package main

import (
    "encoding/xml"
    "fmt"
)

const (
  s = `<?xml version="1.0" encoding="UTF-8"?>
        <feed>
            <product>
                <description>
                 TEST VALUE sdfsdfsdfsdfsd   TEST VALUE sdfsdfsdfsdfsd   TEST VALUE sdfsdfsdfsdfsd   TEST VALUE sdfsdfsdfsdfsd    &#031;
                </description>
                <sku>ABCDD!@#</sku>
            </product>  
       </feed>`
)

type (
    Feed struct {
        XMLName xml.Name `xml:"feed"`
        Product Product  `xml:"product"`
    }

    Product struct {
        XMLName     xml.Name `xml:"product"`
        Description string   `xml:"description"`
        SKU         string   `xml:"sku"`
    }
)

    func main() {
        fmt.Println("Hello, playground")
        b := []byte(s)
        var feed Feed
        xml.Unmarshal(b, &feed)
        fmt.Println(feed.Product.Description)
        fmt.Println(feed.Product.SKU)
    }

Upvotes: 0

Views: 2303

Answers (1)

Simon Whitehead
Simon Whitehead

Reputation: 65077

Don't ignore your errors

Your input data is invalid.

xml.Unmarshal returns an error if it fails. Replacing your call with this:

if err != nil {
    fmt.Println(err)
}

...shows:

XML syntax error on line 6: illegal character code U+001F

Removing the &#031 from your input makes it work.

See it on the Go Playground

Upvotes: 3

Related Questions