我正在做一个命令行应用程序,它充当一些 SOAP 服务的接口。为了发送和接收一些有效的响应,我必须解析一个自定义 xml(信封),每个肥皂服务都有自己的框架信封,并且在那个框架中我必须添加我的 buff/text/info。
一帧看起来像这样。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webPosRo.uaic/">
<soapenv:Body>
<web:parseText_XML>
<rawTextInput>HERE</rawTextInput>
</web:parseText_XML>
</soapenv:Body>
如果您查看“这里”,我必须放置我要发送的内容。我发现使用 encoding/xml 包很奇怪,因为我有 6 个服务,例如每个服务我有一个信封类型。
为了通过它们,我需要像这样制作 6 对不同的结构。
type Envelope struct {
XMLName xml.Name `xml:"Envelope"`
Val1 string `xml:"xmlns:soapenv,attr"`
Val2 string `xml:"xmlns:web,attr"`
CreateBody Body `xml:"soapenv:Body"`
}
type Body struct {
CreateText Text `xml:"web:parseText_XML"`
}
type Text struct {
TextRow []byte `xml:"rawTextInput"`
}
如果我有另一个像这样的信封。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webNpChunkerRo.uaic/">
<soapenv:Body>
<web:chunkText>
<inputText>
</inputText>
</web:chunkText>
</soapenv:Body>
</soapenv:Envelope>
我还有另外 3 个结构对类型。
type Envelope1 struct {
XMLName xml.Name `xml:"Envelope"`
Val1 string `xml:"xmlns:soapenv,attr"`
Val2 string `xml:"xmlns:web,attr"`
CreateBody Body1 `xml:"soapenv:Body"`
}
type Body1 struct {
CreateText Text1 `xml:"web:chunkTest"`
}
type Text1 struct {
TypeRow []byte `xml:"inputText"`
}
而且我觉得这很奇怪..而且为了解析第一个具有命名空间的节点
<soapenv:Envelope ... >
//content
</soapenv:Envelope>
在我 Unmarshall 和 Marshall 之后,我得到
<Envelope ... >
//content
</Envelope>
只是第一个注释丢失了该命名空间“soapenv”,为了让它完整,我必须创建一个像这样对其进行清理的函数。
func sanitizeEnvelope(buffer []byte) []byte {
var (
StartF = []byte("<Envelope")
FinalF = []byte("</Envelope>")
StartT = []byte("<soapenv:Envelope ")
FinalT = []byte("</soapenv:Envelope>")
)
// Check all the bytes equal to StartF and FinalF
// And replace all with StartT and FinalT
buffer = bytes.Replace(buffer, StartF, StartT, -1)
buffer = bytes.Replace(buffer, FinalF, FinalT, -1)
// return the new sanitize envelope buffer
return buffer
}
有没有更好的解决方案来解析这个并且还包括第一个节点命名空间?或者像上面这样的消毒解决方案可以吗?