英文:
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,
<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>
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:"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 // 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 (
"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)
}
Output:
{{ details} { 11 John 20000} { 10 1000 20211407} { 200 Available 10}}
10
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论