dongxiandi8313 2017-06-16 03:37
浏览 38
已采纳

在Go中解组可返回空白输出

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) .

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);

}

A sample of the interface.xml file is :

<?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>

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

</div>
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 在线教育培训平台,主要以点播视频和在线练习测试为主,除将视频做切片外,有其他哪种方式降低流量?(关键词-带宽速率)
      • ¥20 用c语音或c++实现银行叫号系统
      • ¥15 人工智能 规则正向演绎和推理
      • ¥20 基于STM32F401的电子密码锁设计
      • ¥15 famamacbeth回归中遇到only size-1 arrays can be converted to Python scalars,求解答
      • ¥15 单片机多个自锁按键的编程实践
      • ¥15 用python操作redis存储中文后,再取出的数据变成了乱码怎么办?
      • ¥15 C语言简单排序问题有偿求解
      • ¥20 请问图片的代码什么意思
      • ¥15 coq问题求带,有偿