如何从返回的字符串中提取特定字段?

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

how to Extract a particular field from the return string

问题

在命中服务后,能够获得一个返回字符串。字符串如下所示:

<details>
<unitdetails>
<number>11</number>
<name>John</name>
<sal>20000</sal>
</unitdetails>
<deptdetails>
<number>10</number>     <<<<<<<<<<<<
<amt>1000</amt>
<date>20211407</date>
</deptdetails>
<divdetails>
<number>200</number>
<status>Available</status>
<value>10</value>
</divdetails>
</details>

从上述输出字符串中,我需要提取deptdetails下面的数字10

英文:

Was able to get a return string after hitting the service.
String looks as below,

&lt;details&gt;
&lt;unitdetails&gt;
&lt;number&gt;11&lt;/number&gt;
&lt;name&gt;John&lt;/name&gt;
&lt;sal&gt;20000&lt;/sal&gt;
&lt;/unitdetails&gt;
&lt;deptdetails&gt;
&lt;number&gt;10&lt;/number&gt;     &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
&lt;amt&gt;1000&lt;/amt&gt;
&lt;date&gt;20211407&lt;/date&gt;
&lt;/deptdetails&gt;
&lt;divdetails&gt;
&lt;number&gt;200&lt;/number&gt;
&lt;status&gt;Available&lt;/status&gt;
&lt;value&gt;10&lt;/value&gt;
&lt;/divdetails&gt;
&lt;/details&gt;

From the above output string I need to extract only the number '10' under the deptdetails.

答案1

得分: 3

基本上,你可以像其他语言一样进行反序列化。你需要创建一个包含其他结构体的结构体,然后进行反序列化。

参考这个链接https://tutorialedge.net/golang/parsing-xml-with-golang/,里面有一个示例,展示了如何进行反序列化。

type Details struct {
	XMLName     xml.Name `xml:"details"`
	Text        string   `xml:",chardata"`
	Unitdetails struct {
		Text   string `xml:",chardata"`
		Number string `xml:"number"`
		Name   string `xml:"name"`
		Sal    string `xml:"sal"`
	} `xml:"unitdetails"`
	Deptdetails struct {
		Text   string `xml:",chardata"`
		Number string `xml:"number"`
		Amt    string `xml:"amt"`
		Date   string `xml:"date"`
	} `xml:"deptdetails"`
	Divdetails struct {
		Text   string `xml:",chardata"`
		Number string `xml:"number"`
		Status string `xml:"status"`
		Value  string `xml:"value"`
	} `xml:"divdetails"`
}

var details Details
xml.Unmarshal(byteValue, &details)
details.DeptDetails.Number // 访问

这只是一个快速的示例(未经测试!),给你一个想法。

英文:

Basically you would deserialize like in other languages also. So you would create a struct containing other structs and then deserialize.

See eg https://tutorialedge.net/golang/parsing-xml-with-golang/ for an example on how to do this

type Details struct {
	XMLName     xml.Name `xml:&quot;details&quot;`
	Text        string   `xml:&quot;,chardata&quot;`
	Unitdetails struct {
		Text   string `xml:&quot;,chardata&quot;`
		Number string `xml:&quot;number&quot;`
		Name   string `xml:&quot;name&quot;`
		Sal    string `xml:&quot;sal&quot;`
	} `xml:&quot;unitdetails&quot;`
	Deptdetails struct {
		Text   string `xml:&quot;,chardata&quot;`
		Number string `xml:&quot;number&quot;`
		Amt    string `xml:&quot;amt&quot;`
		Date   string `xml:&quot;date&quot;`
	} `xml:&quot;deptdetails&quot;`
	Divdetails struct {
		Text   string `xml:&quot;,chardata&quot;`
		Number string `xml:&quot;number&quot;`
		Status string `xml:&quot;status&quot;`
		Value  string `xml:&quot;value&quot;`
	} `xml:&quot;divdetails&quot;`
} 

var details Details
xml.Unmarshal(byteValue, &amp;details)
details.DeptDetails.Number // access

This is just a quick (and untested!) writedown to give you an idea

答案2

得分: 2

根据Sascha的输入,我为您的场景创建了一个简单的程序,如下所示:

package main

import (
	"encoding/xml"
	"fmt"
)

type Details struct {
	XMLName     xml.Name `xml:"details"`
	Text        string   `xml:",chardata"`
	Unitdetails struct {
		Text   string `xml:",chardata"`
		Number string `xml:"number"`
		Name   string `xml:"name"`
		Sal    string `xml:"sal"`
	} `xml:"unitdetails"`
	Deptdetails struct {
		Text   string `xml:",chardata"`
		Number string `xml:"number"`
		Amt    string `xml:"amt"`
		Date   string `xml:"date"`
	} `xml:"deptdetails"`
	Divdetails struct {
		Text   string `xml:",chardata"`
		Number string `xml:"number"`
		Status string `xml:"status"`
		Value  string `xml:"value"`
	} `xml:"divdetails"`
}

func main() {

	var payload = []byte(`
	<details><unitdetails><number>11</number><name>John</name><sal>20000</sal></unitdetails><deptdetails><number>10</number><amt>1000</amt><date>20211407</date></deptdetails><divdetails><number>200</number><status>Available</status><value>10</value></divdetails></details>
	`)

	var details Details
	xml.Unmarshal(payload, &details)

	fmt.Println(details)
	fmt.Println(details.Deptdetails.Number)
}

输出:

{{ details}  { 11 John 20000} { 10 1000 20211407} { 200 Available 10}}
10
英文:

Based on the input from Sascha,I created a simple program for your scenario as follows :

package main

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

type Details struct {
	XMLName     xml.Name `xml:&quot;details&quot;`
	Text        string   `xml:&quot;,chardata&quot;`
	Unitdetails struct {
		Text   string `xml:&quot;,chardata&quot;`
		Number string `xml:&quot;number&quot;`
		Name   string `xml:&quot;name&quot;`
		Sal    string `xml:&quot;sal&quot;`
	} `xml:&quot;unitdetails&quot;`
	Deptdetails struct {
		Text   string `xml:&quot;,chardata&quot;`
		Number string `xml:&quot;number&quot;`
		Amt    string `xml:&quot;amt&quot;`
		Date   string `xml:&quot;date&quot;`
	} `xml:&quot;deptdetails&quot;`
	Divdetails struct {
		Text   string `xml:&quot;,chardata&quot;`
		Number string `xml:&quot;number&quot;`
		Status string `xml:&quot;status&quot;`
		Value  string `xml:&quot;value&quot;`
	} `xml:&quot;divdetails&quot;`
}

func main() {

	var payload = []byte(`
   	&lt;details&gt;&lt;unitdetails&gt;&lt;number&gt;11&lt;/number&gt;&lt;name&gt;John&lt;/name&gt;&lt;sal&gt;20000&lt;/sal&gt;&lt;/unitdetails&gt;&lt;deptdetails&gt;&lt;number&gt;10&lt;/number&gt;&lt;amt&gt;1000&lt;/amt&gt;&lt;date&gt;20211407&lt;/date&gt;&lt;/deptdetails&gt;&lt;divdetails&gt;&lt;number&gt;200&lt;/number&gt;&lt;status&gt;Available&lt;/status&gt;&lt;value&gt;10&lt;/value&gt;&lt;/divdetails&gt;&lt;/details&gt;

`)

	var details Details
	xml.Unmarshal(payload, &amp;details)

	fmt.Println(details)
	fmt.Println(details.Deptdetails.Number)
}

Output:

{{ details}  { 11 John 20000} { 10 1000 20211407} { 200 Available 10}}
10

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

发表评论

匿名网友

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

确定