猿问

如何从 XML 中提取数据

我正在努力从xml输出中提取数据。我写了下面的代码。我只需要从下面的xml中提取部门编号。在运行以下代码以获取空输出时。有人可以让我如何从xml中提取部门编号以及为什么我得到空作为输出吗?


package main


import (

    "encoding/xml"

    "fmt"

)


type Users struct {

    XMLName xml.Name `xml:"users"`

    User   User   `xml:"user"`

}

type User struct {

    XMLName xml.Name `xml:"user"` 

    Type string  `xml:"name"` 

    DD  DD   `xml:"dd"`

}

type DD struct {

    XMLName xml.Name `xml:"dd"` 

    Number string  `xml:"number"`

    Description string  `xml:"description"` 

   Type  Type   `xml:"type"`

    Dept  Dept   `xml:"dept"`

}

type Type struct{

   XMLName xml.Name `xml:"type"` 

}

type Dept struct {

    XMLName xml.Name `xml:"dept"`

    Number string  `xml:"number"`

   Type  Type   `xml:"type"`

}


func main() {

var users Users

var byteValue = []byte(`<users>

<user>

<type>1</type>

<bu>

    <number>123</number>

    <id>100</id>

    <type>

        <code>123</code>

    </type>

</bu>

<dd>

    <number>1</number>

    <description>abc</description>

    <type>

        <code>12345</code>

        <id>qw123<id>

    <type>

    <dept>

        <number>10</number>      <<<<<<<

        <type>qw12345</type>

        

    </dept>

</dd>

<bd>

    <code>34we5</code>

    <id>qw123<id>

</bd>

<OD>

    <code>9876</code>

    <id>qwerty123<id>

</OD>   

</user>

</users>`)

xml.Unmarshal(byteValue, &users)

fmt.Println("Dept Number: " + users.User.DD.Dept.Number)

}


aluckdog
浏览 119回答 1
1回答

哆啦的时光机

看起来提供的 XML 是错误的。请尝试使用下面的 XML<users>&nbsp; &nbsp; <user>&nbsp; &nbsp; &nbsp; &nbsp; <type>1</type>&nbsp; &nbsp; &nbsp; &nbsp; <bu>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <number>123</number>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>100</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code>123</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </type>&nbsp; &nbsp; &nbsp; &nbsp; </bu>&nbsp; &nbsp; &nbsp; &nbsp; <dd>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <number>1</number>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <description>abc</description>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code>12345</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>qw123</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <dept>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <number>10</number>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <type>qw12345</type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dept>&nbsp; &nbsp; &nbsp; &nbsp; </dd>&nbsp; &nbsp; &nbsp; &nbsp; <bd>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code>34we5</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>qw123</id>&nbsp; &nbsp; &nbsp; &nbsp; </bd>&nbsp; &nbsp; &nbsp; &nbsp; <OD>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code>9876</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>qwerty123</id>&nbsp; &nbsp; &nbsp; &nbsp; </OD>&nbsp; &nbsp; </user></users>您可以在操场上查看工作示例以及您提供的示例。https://play.golang.org/p/4zQsaz5Z_5P
随时随地看视频慕课网APP

相关分类

Go
我要回答