这里怎么给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>`
谢谢,看了你的方法解决了我的问题。可是不懂xmlOutPutData := append(headerBytes, xmlOutPut...) 请问下 为什么要加...?
#4
更多评论
```
//把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