how to Extracting data from XML

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

how to Extracting data from 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)
}
英文:

I am working on extracting data from a xml output.
I have written the below code. I just need to extract dept number from the below xml.
On running the below code getting a null output.
Can someone let me how to extract the dept number from the xml and why I am getting null as output?

package main

import (
    &quot;encoding/xml&quot;
    &quot;fmt&quot;
)

type Users struct {
    XMLName xml.Name `xml:&quot;users&quot;`
    User   User   `xml:&quot;user&quot;`
}
type User struct {
    XMLName xml.Name `xml:&quot;user&quot;` 
    Type string  `xml:&quot;name&quot;` 
    DD  DD   `xml:&quot;dd&quot;`
}
type DD struct {
    XMLName xml.Name `xml:&quot;dd&quot;` 
    Number string  `xml:&quot;number&quot;`
    Description string  `xml:&quot;description&quot;` 
   Type  Type   `xml:&quot;type&quot;`
    Dept  Dept   `xml:&quot;dept&quot;`
}
type Type struct{
   XMLName xml.Name `xml:&quot;type&quot;` 
}
type Dept struct {
    XMLName xml.Name `xml:&quot;dept&quot;`
    Number string  `xml:&quot;number&quot;`
   Type  Type   `xml:&quot;type&quot;`
}

func main() {
var users Users
var byteValue = []byte(`&lt;users&gt;
&lt;user&gt;
&lt;type&gt;1&lt;/type&gt;
&lt;bu&gt;
	&lt;number&gt;123&lt;/number&gt;
	&lt;id&gt;100&lt;/id&gt;
	&lt;type&gt;
		&lt;code&gt;123&lt;/code&gt;
	&lt;/type&gt;
&lt;/bu&gt;
&lt;dd&gt;
	&lt;number&gt;1&lt;/number&gt;
	&lt;description&gt;abc&lt;/description&gt;
	&lt;type&gt;
		&lt;code&gt;12345&lt;/code&gt;
		&lt;id&gt;qw123&lt;id&gt;
	&lt;type&gt;
	&lt;dept&gt;
		&lt;number&gt;10&lt;/number&gt;      &lt;&lt;&lt;&lt;&lt;&lt;&lt;
		&lt;type&gt;qw12345&lt;/type&gt;
		
	&lt;/dept&gt;
&lt;/dd&gt;
&lt;bd&gt;
	&lt;code&gt;34we5&lt;/code&gt;
	&lt;id&gt;qw123&lt;id&gt;
&lt;/bd&gt;
&lt;OD&gt;
	&lt;code&gt;9876&lt;/code&gt;
	&lt;id&gt;qwerty123&lt;id&gt;
&lt;/OD&gt;	
&lt;/user&gt;
&lt;/users&gt;`)
xml.Unmarshal(byteValue, &amp;users)
fmt.Println(&quot;Dept Number: &quot; + users.User.DD.Dept.Number)
}

答案1

得分: 2

看起来提供的 XML 是错误的。请尝试使用以下 XML:

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

您可以在 playground 上查看使用您提供的示例的工作示例。链接:https://play.golang.org/p/4zQsaz5Z_5P

英文:

Looks like the XML provided is wrong. Please do try using below XML

&lt;users&gt;
&lt;user&gt;
&lt;type&gt;1&lt;/type&gt;
&lt;bu&gt;
&lt;number&gt;123&lt;/number&gt;
&lt;id&gt;100&lt;/id&gt;
&lt;type&gt;
&lt;code&gt;123&lt;/code&gt;
&lt;/type&gt;
&lt;/bu&gt;
&lt;dd&gt;
&lt;number&gt;1&lt;/number&gt;
&lt;description&gt;abc&lt;/description&gt;
&lt;type&gt;
&lt;code&gt;12345&lt;/code&gt;
&lt;id&gt;qw123&lt;/id&gt;
&lt;/type&gt;
&lt;dept&gt;
&lt;number&gt;10&lt;/number&gt;
&lt;type&gt;qw12345&lt;/type&gt;
&lt;/dept&gt;
&lt;/dd&gt;
&lt;bd&gt;
&lt;code&gt;34we5&lt;/code&gt;
&lt;id&gt;qw123&lt;/id&gt;
&lt;/bd&gt;
&lt;OD&gt;
&lt;code&gt;9876&lt;/code&gt;
&lt;id&gt;qwerty123&lt;/id&gt;
&lt;/OD&gt;
&lt;/user&gt;
&lt;/users&gt;

You can have a look at the working example with your provided example here on playground. https://play.golang.org/p/4zQsaz5Z_5P

huangapple
  • 本文由 发表于 2021年7月19日 16:24:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/68437317.html
匿名

发表评论

匿名网友

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

确定