这里怎么给username 和password传参 ,请求大神指教~谢谢
reqBody := `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<login xmlns="http://xxx.xx.x.xx/SOAP">
<struct>
<username>username</username><password>password</password>
</struct>
</login>
</soap:Body>
</soap:Envelope>`
```
//把xml文件保存为version.xml
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
)
type Context struct {
XMLName xml.Name
Body Body
}
type Body struct {
XMLName xml.Name
Login Login `xml:"login"`
}
type Login struct {
XMLName xml.Name `xml:"login"`
Id string `xml:"xmlns,attr"`
Inner Inner `xml:"struct"`
}
type Inner struct {
XMLName xml.Name `xml:"struct"`
UserName string `xml:"username"`
Password string `xml:"password"`
}
func main() {
content, err := ioutil.ReadFile("version.xml")
if err != nil {
log.Fatal(err)
}
res := new(Context)
err = xml.Unmarshal(content, res)
res.Body.Login.Inner.UserName = "你想修改的username1"
res.Body.Login.Inner.Password = "你想修改的password1"
xmlOutPut, outPutErr := xml.MarshalIndent(res, "", "")
if outPutErr == nil {
//加入XML头
headerBytes := []byte(xml.Header)
//拼接XML头和实际XML内容
xmlOutPutData := append(headerBytes, xmlOutPut...)
//写入文件
ioutil.WriteFile("version.xml", xmlOutPutData, os.ModeAppend)
fmt.Println("OK~")
} else {
fmt.Println(outPutErr)
}
}
```
#2
更多评论
谢谢哈,我已经解决了,但是遇到一个问题,就是os.Stdout.Write(xmlOutPut) 之后形成的是标准输出,我想以string的形式return给其他函数调用。请问,我要怎么处理,我看了下说byte.buffer可以,但是一直没搞明白。
#3