FAQ
Why can not correctly read xml?

package main

import (
"encoding/xml"
"testing"
)

type XMLTest struct {
XMLName xml.Name `xml:"test"`
Ts []XMLEntity
}

type XMLEntity struct {
XMLName xml.Name `xml:"t"`
Value string `xml:"value"`
}

func TestXml(t *testing.T) {
var t1, t2 XMLTest
t1.Ts = []XMLEntity{XMLEntity{Value: "a"}, XMLEntity{Value: "b"}}
txt, _ := xml.Marshal(&t1)
err := xml.Unmarshal(txt, &t2)
if nil != err {
t.Error("read xml failed.")
}

if nil == t2.Ts || 0 == len(t2.Ts) {
t.Log(string(txt))
t.Error("test xml failed.")
}
}


Above code run to return the following error

--- FAIL: TestXml (0.00 seconds)
xml_test.go:28:
<test><t><value>a</value></t><t><value>b</value></t></test>
xml_test.go:29: test xml failed.
FAIL
exit status 1
FAIL bug 0.071s

--

Search Discussions

  • Dave Cheney at Jan 5, 2013 at 5:44 am
    Use t.Errorf so you can see the error

    err := xml.Unmarshal(txt, &t2)
    if nil != err {
    t.Errorf("read xml failed: %v", err)
    }
    On Sat, Jan 5, 2013 at 4:19 PM, runner mei wrote:
    err := xml.Unmarshal(txt, &t2)
    if nil != err {
    t.Error("read xml failed.")
    }
    --
  • Runner mei at Jan 5, 2013 at 6:01 am
    No, erro lineno is 29, it is
    if nil == t2.Ts || 0 == len(t2.Ts) {
    t.Log(string(txt))
    t.Error("test xml failed.")
    }

    在 2013年1月5日星期六UTC+8下午1时44分29秒,Dave Cheney写道:
    Use t.Errorf so you can see the error

    err := xml.Unmarshal(txt, &t2)
    if nil != err {
    t.Errorf("read xml failed: %v", err)
    }
    On Sat, Jan 5, 2013 at 4:19 PM, runner mei wrote:
    err := xml.Unmarshal(txt, &t2)
    if nil != err {
    t.Error("read xml failed.")
    }
    --
  • Cole Mickens at Jan 5, 2013 at 7:06 am
    I repeatedly get bit by this in the XML package. It's quite particular
    about how you annotate its names.
    http://play.golang.org/p/jKZR4HcH7y

    I also experienced something where it appeared to encode correctly but not
    decode. Anyhow, this doesn't spit out any more errors as it tells the XML
    package that your sub-entity is named by 't'. Previously, in your code, it
    would be looking for sub-elements named 'Ts' if I understand correctly.
    On Friday, January 4, 2013 11:19:30 PM UTC-6, runner mei wrote:

    Why can not correctly read xml?

    package main

    import (
    "encoding/xml"
    "testing"
    )

    type XMLTest struct {
    XMLName xml.Name `xml:"test"`
    Ts []XMLEntity
    }

    type XMLEntity struct {
    XMLName xml.Name `xml:"t"`
    Value string `xml:"value"`
    }

    func TestXml(t *testing.T) {
    var t1, t2 XMLTest
    t1.Ts = []XMLEntity{XMLEntity{Value: "a"}, XMLEntity{Value: "b"}}
    txt, _ := xml.Marshal(&t1)
    err := xml.Unmarshal(txt, &t2)
    if nil != err {
    t.Error("read xml failed.")
    }

    if nil == t2.Ts || 0 == len(t2.Ts) {
    t.Log(string(txt))
    t.Error("test xml failed.")
    }
    }


    Above code run to return the following error

    --- FAIL: TestXml (0.00 seconds)
    xml_test.go:28:
    <test><t><value>a</value></t><t><value>b</value></t></test>
    xml_test.go:29: test xml failed.
    FAIL
    exit status 1
    FAIL bug 0.071s
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 5, '13 at 5:19a
activeJan 5, '13 at 7:06a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase