Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: nested decode fails #1989

Closed
gopherbot opened this Issue Jun 21, 2011 · 7 comments

Comments

Projects
None yet
5 participants
@gopherbot
Copy link

gopherbot commented Jun 21, 2011

by jdnurmi@qwe.cc:

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull -u" and retry what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?
1.  Grab a very large (deep) XML document with complex types.
2.  Attempt to use  'tag' path specifications after the 3rd level or so.
3.  Tag directives are ignored, leading to un-parsed variables or potentially misplaced
values.

Below is a small sample I've contrived that demonstrates the issue.


What is the expected output?
    xml correctly decoded our bad idea!

What do you see instead?
     xml Decoded into the 'Name' not the 'attr'!


Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
linux

Which revision are you using?  (hg identify)


Please provide any additional information below.



package main

import (
  "bytes"
  "fmt"
  "xml"
)

type T1 struct {
  Children []T2 "children>kid"
}

type T2 struct {
  Children []T3 "children>kid"
}

// Notice the swap of variables here!
type T3 struct {
  X string "y"
  Y string "x"
}

func main(){
  v := T1{}
  err := xml.Unmarshal(bytes.NewBufferString("<root><children><kid><children><kid><X>RealX</X><Y>RealY</Y></kid></children></kid></children></root>"), &v)
  if err == nil {
    if v.Children[0].Children[0].X != "RealY" {
      fmt.Printf("xml Decoded into the 'Name' not the 'attr'!\n")
    } else {
      fmt.Printf("xml correctly decoded our bad idea!\n")
    }
    fmt.Printf("(Val == %+v)\n", v)
  } else {
    fmt.Printf("Err == %+v\n", err)
  }
}




I'm not sure whether this is actually an xml issue or simply a limitation of go's
reflection since I'm not using pointer types, but knowing which will allow me to figure
out the correct approach.
@robpike

This comment has been minimized.

Copy link
Contributor

robpike commented Jul 6, 2011

Comment 1:

Owner changed to @rsc.

Status changed to Accepted.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Oct 6, 2011

Comment 2:

Status changed to HelpWanted.

@davecheney

This comment has been minimized.

Copy link
Contributor

davecheney commented Oct 8, 2011

Comment 5:

Hi,
I'm not sure if the xml library supports overriding the name of the member field an
element is assigned too via it's tag. If it does, then this is a simpler test case which
reproduces the issue.
--- a/src/pkg/xml/read_test.go  Thu Oct 06 07:22:48 2011 +0100
+++ b/src/pkg/xml/read_test.go  Sat Oct 08 15:53:41 2011 +1100
@@ -369,3 +369,30 @@
                Bool: true,
        },
 }
+
+type TestNamed1 struct {
+       A string `xml:"B"`
+       B string `xml:"A"`
+}
+
+const testNamed1String = `
+<?xml version="1.0" charset="utf-8"?>
+<TestNamed1>
+  <A>b</A>
+  <B>a</B>
+</TestNamed1>
+`
+
+func TestUnmarshalNamed(t *testing.T) {
+       var actual TestNamed1
+       var expected = TestNamed1{
+               A: "a",
+               B: "b",
+       }
+       if err := Unmarshal(StringReader(testNamed1String), &actual); err != nil {
+                t.Fatalf("Unmarshal: %s", err)
+        }
+        if !reflect.DeepEqual(actual, expected) {
+                t.Fatalf("have %#v\nwant %#v", actual, expected)
+        }
+}
@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 9, 2011

Comment 6:

Labels changed: added priority-later.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 12, 2011

Comment 7:

Labels changed: added priority-go1.

@niemeyer

This comment has been minimized.

Copy link
Contributor

niemeyer commented Dec 29, 2011

Comment 8:

Owner changed to @niemeyer.

Status changed to Started.

@niemeyer

This comment has been minimized.

Copy link
Contributor

niemeyer commented Jan 13, 2012

Comment 9:

This issue was closed by revision 1627b46.

Status changed to Fixed.

@mikioh mikioh changed the title xml: nested decode fails encoding/xml: nested decode fails Jan 9, 2015

@rsc rsc added this to the Go1 milestone Apr 10, 2015

@rsc rsc removed the priority-go1 label Apr 10, 2015

@golang golang locked and limited conversation to collaborators Jun 24, 2016

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.