encoding/xml bug? panic: reflect.Set: value of type []uint8 is not assignable to type []string

255 views
Skip to first unread message

Tong Sun

unread,
Nov 4, 2015, 11:40:27 AM11/4/15
to golang-nuts

I'm experiencing a string problem -- please take a look at this simple demo


It clearly shows that QueryStringParameters.QueryStringParameter can be further break down into an array. However, if I change line 44 to that like line 42, I'll get the error of,

    panic: reflect.Set: value of type []uint8 is not assignable to type []string

Am I doing something wrong? 

Thanks


James Bardin

unread,
Nov 4, 2015, 12:26:53 PM11/4/15
to golang-nuts
The innerxml tag returns the raw xml, it doesn't parse it, so it can't split the two elements into two strings. If you want the individual elements, create a QueryStringParameter type to unmarshal into.  

Tong Sun

unread,
Nov 4, 2015, 12:41:22 PM11/4/15
to James Bardin, golang-nuts
Regardless, again, here is what I want to achieve:

  <QueryStringParameters>
    <QueryStringParameter Name="v" Value="SoftwareVersion" RecordedValue="" CorrelationBinding="" UrlEncode="True" UseToGroupResults="False" />
    <QueryStringParameter Name="xref" Value="RRR" RecordedValue="" CorrelationBinding="" UrlEncode="True" UseToGroupResults="False" />
  </QueryStringParameters>

Given above, the QueryStringParameters.QueryStringParameter should be an array, regardless what I want from it, whether it is just the Name field, or the whole xml string. 

I.e., at the QueryStringParameter level, I should get an array of two elements (Name field only, or the whole xml string). How can I do that? 

Thanks

Tong Sun

unread,
Nov 4, 2015, 12:49:08 PM11/4/15
to James Bardin, golang-nuts
Hmm... I kind of get it now. Looking into the new direction now...

Tong Sun

unread,
Nov 4, 2015, 1:13:16 PM11/4/15
to golang-nuts
Hi, 

When parsing XML, say at the level of 
 
   <QueryStringParameter Name="v" Value="SoftwareVersion" ... />

How I can get the full xml string ("<QueryStringParameter Name= ... />") at this level? The innerxml tag only gives me empty string. Details below. 

I'm experiencing a strange problem -- please take a look at this simple demo


It clearly shows that QueryStringParameters.QueryStringParameter can be further break down into an array. However, if I change line 44 to that like line 42, I'll get the error of,

    panic: reflect.Set: value of type []uint8 is not assignable to type []string

Am I doing something wrong? 
The innerxml tag returns the raw xml, it doesn't parse it, so it can't split the two elements into two strings. If you want the individual elements, create a QueryStringParameter type to unmarshal into.  

..., here is what I want to achieve:

  <QueryStringParameters>
    <QueryStringParameter Name="v" Value="SoftwareVersion" RecordedValue="" CorrelationBinding="" UrlEncode="True" UseToGroupResults="False" />
    <QueryStringParameter Name="xref" Value="RRR" RecordedValue="" CorrelationBinding="" UrlEncode="True" UseToGroupResults="False" />
  </QueryStringParameters>

Given above, the QueryStringParameters.QueryStringParameter should be an array, regardless what I want from it, whether it is just the Name field, or the whole xml string. 

I.e., at the QueryStringParameter level, I should get an array of two elements (Name field only, or the whole xml string). How can I do that? 
 
Thanks
 

James Bardin

unread,
Nov 4, 2015, 2:16:17 PM11/4/15
to golang-nuts


On Wednesday, November 4, 2015 at 1:13:16 PM UTC-5, Tong Sun wrote:
Hi, 

When parsing XML, say at the level of 
 
   <QueryStringParameter Name="v" Value="SoftwareVersion" ... />

How I can get the full xml string ("<QueryStringParameter Name= ... />") at this level? The innerxml tag only gives me empty string.

That element is empty, it has no inner xml. If you want the raw xml, you have to go up one level. 

You can create a type to get whatever attributes you need: https://play.golang.org/p/AyzmoUmVYS

If you want the raw data, or attribute that you don't know about ahead of time, you need to implement an xml.Unmarshaler

Tong Sun

unread,
Nov 4, 2015, 2:40:29 PM11/4/15
to James Bardin, golang-nuts
On Wed, Nov 4, 2015 at 2:16 PM, James Bardin <j.ba...@gmail.com> wrote:


On Wednesday, November 4, 2015 at 1:13:16 PM UTC-5, Tong Sun wrote:
Hi, 

When parsing XML, say at the level of 
 
   <QueryStringParameter Name="v" Value="SoftwareVersion" ... />

How I can get the full xml string ("<QueryStringParameter Name= ... />") at this level? The innerxml tag only gives me empty string.

... If you want the raw xml, you have to go up one level. 

That's sad, right? The encoding/xml package should have thought of and provided it, giving full xml at that level. Too bad such easy feature is missing from it. 


Matt Harden

unread,
Nov 4, 2015, 7:38:28 PM11/4/15
to Tong Sun, James Bardin, golang-nuts
Nah, just implement xml.Unmarshaler as James said.

This is a difference between encoding/json and encoding/xml, though. The json package has a RawMessage type that basically does what you want, only for JSON. For a hypothetical xml.RawMessage, UnmarshalXML would be easy to implement (so easy that you might as well do it yourself whenever you need it), but MarshalXML would be difficult or potentially impossible because of XML Namespaces. That problem doesn't exist in JSON.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Glen Newton

unread,
Nov 6, 2015, 8:06:51 PM11/6/15
to golang-nuts, sunto...@gmail.com, j.ba...@gmail.com
Or just the xml through chidley and get these structs
chidley https://github.com/gnewton/chidley

type Chiroot struct {
    ChiPerson *ChiPerson `xml:" Person,omitempty" json:"Person,omitempty"`
}

type ChiPerson struct {
    ChiCompany *ChiCompany `xml:" Company,omitempty" json:"Company,omitempty"`
    ChiCondition *ChiCondition `xml:" Condition,omitempty" json:"Condition,omitempty"`
    ChiName *ChiName `xml:" Name,omitempty" json:"Name,omitempty"`
    ChiPerson *ChiPerson `xml:" Person,omitempty" json:"Person,omitempty"`
    ChiQueryStringParameters *ChiQueryStringParameters `xml:" QueryStringParameters,omitempty" json:"QueryStringParameters,omitempty"`
}

type ChiCondition struct {
    Attr_UniqueStringId string `xml:" UniqueStringId,attr"  json:",omitempty"`
    ChiConditionalRule *ChiConditionalRule `xml:" ConditionalRule,omitempty" json:"ConditionalRule,omitempty"`
}

type ChiConditionalRule struct {
    Attr_Classname string `xml:" Classname,attr"  json:",omitempty"`
    Attr_DisplayName string `xml:" DisplayName,attr"  json:",omitempty"`
    Attr_Description string `xml:" Description,attr"  json:",omitempty"`
    ChiRuleParameters *ChiRuleParameters `xml:" RuleParameters,omitempty" json:"RuleParameters,omitempty"`
}

type ChiRuleParameters struct {
    ChiRuleParameter []*ChiRuleParameter `xml:" RuleParameter,omitempty" json:"RuleParameter,omitempty"`
}

type ChiRuleParameter struct {
    Attr_Name string `xml:" Name,attr"  json:",omitempty"`
    Attr_Value string `xml:" Value,attr"  json:",omitempty"`
}

type ChiQueryStringParameters struct {
    ChiQueryStringParameter []*ChiQueryStringParameter `xml:" QueryStringParameter,omitempty" json:"QueryStringParameter,omitempty"`
}

type ChiQueryStringParameter struct {
    Attr_Name string `xml:" Name,attr"  json:",omitempty"`
    Attr_Value string `xml:" Value,attr"  json:",omitempty"`
    Attr_RecordedValue string `xml:" RecordedValue,attr"  json:",omitempty"`
    Attr_CorrelationBinding string `xml:" CorrelationBinding,attr"  json:",omitempty"`
    Attr_UrlEncode string `xml:" UrlEncode,attr"  json:",omitempty"`
    Attr_UseToGroupResults string `xml:" UseToGroupResults,attr"  json:",omitempty"`
}

type ChiName struct {
    Text string `xml:",chardata" json:",omitempty"`
}

type ChiCompany struct {
    ChiAddr *ChiAddr `xml:" Addr,omitempty" json:"Addr,omitempty"`
    ChiName *ChiName `xml:" Name,omitempty" json:"Name,omitempty"`
}

type ChiAddr struct {
    Text string `xml:",chardata" json:",omitempty"`
}


///////////////////////////
Reply all
Reply to author
Forward
0 new messages