JiaChen ZENG
JiaChen ZENG

Reputation: 721

Go: After parse xml file with template.ParseFiles, the first "<" became "<"

I am trying to parse a xml file using template.ParseFiles().

The xml is :

<?xml version="1.0" encoding="utf-8"?>
<in2>
    <unique>{{.}}</unique>
    <moe>100%</moe>
</in2>

But after parsing it, the first < became &lt;, like this :

&lt;?xml version="1.0" encoding="utf-8"?>
<in2>
    <unique>something</unique>
    <moe>100%</moe>
</in2>

How can I parse the xml file correctly?

This is my code :

func in2(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/xml")
    t, err := template.ParseFiles("xml/in2.xml")
    if err != nil {
        fmt.Println(err)
        return
    }
    unique := "something"
    err = t.Execute(w, unique)
    if err != nil {
        fmt.Println(err)
    }
}

Upvotes: 3

Views: 2730

Answers (1)

Jeremy Wall
Jeremy Wall

Reputation: 25245

I don't think html/template understands xml so an xml template is going to give it problems. If you need to work with xml then the http://golang.org/pkg/encoding/xml/ package may be of use.

Or you can use text/template which won't care about your xml. The downside to using text/template is that it won't be context aware but then html/template isn't going to understand the context of your xml either.

Upvotes: 2

Related Questions