在Go语言中,反序列化操作返回一个空输出。

huangapple go评论75阅读模式
英文:

Unmarshalling in Go returns a blank output

问题

我得到了以下打印语句的空白输出。只有第二个打印语句输出了XML结构。这个问题是由我的结构体引起的吗?我是使用Chidley(https://github.com/gnewton/chidley)生成的结构体。

package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
)

type Query struct {
	rpc Rpc_reply `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty" json:"rpc-reply,omitempty"`
}

type Addr_tag_list struct {
	Addr_tag    Addr_tag    `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"`
	Pref_tag    Pref_tag    `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"`
	Tag         Tag         `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"`
	Type_tag    Type_tag    `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"`
	VpcPeer_tag VpcPeer_tag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"`
	XMLName     xml.Name    `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"`
}

type Dom_list struct {
	If_items If_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty" json:"if-items,omitempty"`
	Name_tag Name_tag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty" json:"name,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty" json:"Dom-list,omitempty"`
}

type If_list struct {
	Addr_items_tag Addr_items_tag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty" json:"addr-items,omitempty"`
	Id_tag         Id_tag         `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty" json:"id,omitempty"`
	XMLName        xml.Name       `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty" json:"If-list,omitempty"`
}

type Systemtag struct {
	Ipv4_items Ipv4_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty" json:"ipv4-items,omitempty"`
	Test_name  Test_name  `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty" json:"test-name,omitempty"`
	XMLName    xml.Name   `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty" json:"System,omitempty"`
}

type Addr_tag struct {
	Text    string   `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"`
}

type Addr_items_tag struct {
	Addr_tag_list Addr_tag_list `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"`
	XMLName       xml.Name      `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty" json:"addr-items,omitempty"`
}

type Data_tag struct {
	Systemtag Systemtag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty" json:"System,omitempty"`
	XMLName   xml.Name  `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty" json:"data,omitempty"`
}

type Dom_items struct {
	Dom_list []Dom_list `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty" json:"Dom-list,omitempty"`
	XMLName  xml.Name   `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty" json:"dom-items,omitempty"`
}

type Id_tag struct {
	Text    string   `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty" json:"id,omitempty"`
}

type If_items struct {
	If_list  []If_list `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty" json:"If-list,omitempty"`
	XMLName  xml.Name  `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty" json:"if-items,omitempty"`
}

type Inst_items struct {
	Dom_items Dom_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty" json:"dom-items,omitempty"`
	XMLName   xml.Name  `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty" json:"inst-items,omitempty"`
}

type Ipv4_items struct {
	Inst_items Inst_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty" json:"inst-items,omitempty"`
	XMLName    xml.Name   `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty" json:"ipv4-items,omitempty"`
}

type Name_tag struct {
	Text    string   `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty" json:"name,omitempty"`
}

type Pref_tag struct {
	Text    bool     `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"`
}

type Rpc_reply struct {
	Attr_message_id string   `xml:" message-id,attr"  json:",omitempty"`
	Attr_xmlns      string   `xml:" xmlns,attr"  json:",omitempty"`
	Data_tag        Data_tag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty" json:"data,omitempty"`
	XMLName         xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty" json:"rpc-reply,omitempty"`
}

type Tag struct {
	Text    bool     `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"`
}

type Type_tag struct {
	Text    string   `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"`
}

type VpcPeer_tag struct {
	Text    string   `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"`
}

type Test_name struct {
	Text    string   `xml:",chardata" json:",omitempty"`
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty" json:"test-name,omitempty"`
}

func main() {
	xmlFile, err := os.Open("interface.xml")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer xmlFile.Close()

	b, _ := ioutil.ReadAll(xmlFile)

	var q Query
	err2 := xml.Unmarshal(b, &q)
	if err2 != nil {
		fmt.Printf("error: %v", err2)
		return
	}
	fmt.Println(q)
	//fmt.Printf("%#v",q)
	fmt.Printf("%s", q.rpc.Attr_message_id)

}

接下来是interface.xml文件的示例内容:

<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
  <data>
    <System>
     <test-name>##WORK##</test-name>
      <ipv4-items>
        <inst-items>
          <dom-items>
            <Dom-list>
              <name>default</name>
              <if-items>
                <If-list>
                  <id>eth5/8</id>
                  <addr-items>
                    <Addr-list>
                      <addr>1.1.1.1</addr>
                      <pref>1</pref>
                      <tag>0</tag>
                      <type>primary</type>
                      <vpcPeer>0.0.0.0</vpcPeer>
                    </Addr-list>
                  </addr-items>
                </If-list>
                </if-items>
              </Dom-list>
               <Dom-list>
              <name>management</name>
              <if-items>
                <If-list>
                  <id>eth5/8</id>
                  <addr-items>
                    <Addr-list>
                      <addr>11.11.11.11</addr>
                      <pref>1</pref>
                      <tag>0</tag>
                      <type>primary</type>
                      <vpcPeer>0.0.0.0</vpcPeer>
                    </Addr-list>
                  </addr-items>
                </If-list>
                </if-items>
              </Dom-list>
              </dom-items>
        </inst-items>
      </ipv4-items>
    </System>
  </data>
</rpc-reply>

你可以使用http://countwordsfree.com/xmlviewer加载XML数据以获得更好的视图。

英文:

I get a blank output for the print statements below. Only for the second print statement I get the xml structure. Is the issue with my struct? I have generated the structs using Chidley (https://github.com/gnewton/chidley) .

<!-- begin snippet: js hide: false console: true babel: false -->

package main
import (
&quot;encoding/xml&quot;
&quot;fmt&quot;
&quot;io/ioutil&quot;
&quot;os&quot;
)
type Query struct {
rpc Rpc_reply `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty&quot; json:&quot;rpc-reply,omitempty&quot;`
}
type Addr_tag_list struct {
Addr_tag Addr_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty&quot; json:&quot;addr,omitempty&quot;`
Pref_tag Pref_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty&quot; json:&quot;pref,omitempty&quot;`
Tag Tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty&quot; json:&quot;tag,omitempty&quot;`
Type_tag Type_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty&quot; json:&quot;type,omitempty&quot;`
VpcPeer_tag VpcPeer_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty&quot; json:&quot;vpcPeer,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty&quot; json:&quot;Addr-list,omitempty&quot;`
}
type Dom_list struct {
If_items If_items `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty&quot; json:&quot;if-items,omitempty&quot;`
Name_tag Name_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty&quot; json:&quot;name,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty&quot; json:&quot;Dom-list,omitempty&quot;`
}
type If_list struct {
Addr_items_tag Addr_items_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty&quot; json:&quot;addr-items,omitempty&quot;`
Id_tag Id_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty&quot; json:&quot;id,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty&quot; json:&quot;If-list,omitempty&quot;`
}
type Systemtag struct {
Ipv4_items Ipv4_items `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty&quot; json:&quot;ipv4-items,omitempty&quot;`
Test_name Test_name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty&quot; json:&quot;test-name,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty&quot; json:&quot;System,omitempty&quot;`
}
type Addr_tag struct {
Text string `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty&quot; json:&quot;addr,omitempty&quot;`
}
type Addr_items_tag struct {
Addr_tag_list Addr_tag_list `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty&quot; json:&quot;Addr-list,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty&quot; json:&quot;addr-items,omitempty&quot;`
}
type Data_tag struct {
Systemtag Systemtag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty&quot; json:&quot;System,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty&quot; json:&quot;data,omitempty&quot;`
}
type Dom_items struct {
Dom_list []Dom_list `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty&quot; json:&quot;Dom-list,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty&quot; json:&quot;dom-items,omitempty&quot;`
}
type Id_tag struct {
Text string `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty&quot; json:&quot;id,omitempty&quot;`
}
type If_items struct {
If_list []If_list `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty&quot; json:&quot;If-list,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty&quot; json:&quot;if-items,omitempty&quot;`
}
type Inst_items struct {
Dom_items Dom_items `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty&quot; json:&quot;dom-items,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty&quot; json:&quot;inst-items,omitempty&quot;`
}
type Ipv4_items struct {
Inst_items Inst_items `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty&quot; json:&quot;inst-items,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty&quot; json:&quot;ipv4-items,omitempty&quot;`
}
type Name_tag struct {
Text string `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty&quot; json:&quot;name,omitempty&quot;`
}
type Pref_tag struct {
Text bool `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty&quot; json:&quot;pref,omitempty&quot;`
}
type Rpc_reply struct {
Attr_message_id string `xml:&quot; message-id,attr&quot;  json:&quot;,omitempty&quot;`
Attr_xmlns string `xml:&quot; xmlns,attr&quot;  json:&quot;,omitempty&quot;`
Data_tag Data_tag `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty&quot; json:&quot;data,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty&quot; json:&quot;rpc-reply,omitempty&quot;`
}
type Tag struct {
Text bool `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty&quot; json:&quot;tag,omitempty&quot;`
}
type Type_tag struct {
Text string `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty&quot; json:&quot;type,omitempty&quot;`
}
type VpcPeer_tag struct {
Text string `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty&quot; json:&quot;vpcPeer,omitempty&quot;`
}
type Test_name struct {
Text string `xml:&quot;,chardata&quot; json:&quot;,omitempty&quot;`
XMLName  xml.Name `xml:&quot;urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty&quot; json:&quot;test-name,omitempty&quot;`
}
func main() {
xmlFile, err := os.Open(&quot;interface.xml&quot;)
if err != nil {
fmt.Println(&quot;Error opening file:&quot;, err)
return
}
defer xmlFile.Close()
b, _ := ioutil.ReadAll(xmlFile)
var q Query
err2 := xml.Unmarshal(b, &amp;q)
if err2 != nil {
fmt.Printf(&quot;error: %v&quot;, err2)
return
}
fmt.Println(q)
//fmt.Printf(&quot;%#v&quot;,q)
fmt.Printf(&quot;%s&quot;,q.rpc.Attr_message_id);
}

<!-- end snippet -->

A sample of the interface.xml file is :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;rpc-reply xmlns=&quot;urn:ietf:params:xml:ns:netconf:base:1.0&quot; message-id=&quot;1&quot;&gt;
&lt;data&gt;
&lt;System&gt;
&lt;test-name&gt;##WORK##&lt;/test-name&gt;
&lt;ipv4-items&gt;
&lt;inst-items&gt;
&lt;dom-items&gt;
&lt;Dom-list&gt;
&lt;name&gt;default&lt;/name&gt;
&lt;if-items&gt;
&lt;If-list&gt;
&lt;id&gt;eth5/8&lt;/id&gt;
&lt;addr-items&gt;
&lt;Addr-list&gt;
&lt;addr&gt;1.1.1.1&lt;/addr&gt;
&lt;pref&gt;1&lt;/pref&gt;
&lt;tag&gt;0&lt;/tag&gt;
&lt;type&gt;primary&lt;/type&gt;
&lt;vpcPeer&gt;0.0.0.0&lt;/vpcPeer&gt;
&lt;/Addr-list&gt;
&lt;/addr-items&gt;
&lt;/If-list&gt;
&lt;/if-items&gt;
&lt;/Dom-list&gt;
&lt;Dom-list&gt;
&lt;name&gt;management&lt;/name&gt;
&lt;if-items&gt;
&lt;If-list&gt;
&lt;id&gt;eth5/8&lt;/id&gt;
&lt;addr-items&gt;
&lt;Addr-list&gt;
&lt;addr&gt;11.11.11.11&lt;/addr&gt;
&lt;pref&gt;1&lt;/pref&gt;
&lt;tag&gt;0&lt;/tag&gt;
&lt;type&gt;primary&lt;/type&gt;
&lt;vpcPeer&gt;0.0.0.0&lt;/vpcPeer&gt;
&lt;/Addr-list&gt;
&lt;/addr-items&gt;
&lt;/If-list&gt;
&lt;/if-items&gt;
&lt;/Dom-list&gt;
&lt;/dom-items&gt;
&lt;/inst-items&gt;
&lt;/ipv4-items&gt;
&lt;/System&gt;
&lt;/data&gt;
&lt;/rpc-reply&gt;

<!-- end snippet -->

You may load the xml data http://countwordsfree.com/xmlviewer to get a better view

答案1

得分: 0

问题是您传递的结构体在XML层次结构中没有元素(XMLName xml.Name映射)。

将这两行代码更改为:

var rpc Rpc_reply
err2 := xml.Unmarshal(b, &rpc)
英文:

Issue is struct you're passing doesn't have element in the XML hierarchy (XMLName xml.Name mapping).

Change these two lines to-

var rpc Rpc_reply
err2 := xml.Unmarshal(b, &amp;rpc)

huangapple
  • 本文由 发表于 2017年6月16日 11:37:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/44580389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定