package main
import (
"fmt"
"encoding/xml"
)
type Envelope struct {
XMLName xml.Name
Header Header
}
type Header struct {
XMLName xml.Name `xml:"Header"`
Security Security `xml:"Security"`
}
type Security struct {
XMLName xml.Name `xml:"Security"`
MustUnderstand string `xml:"mustUnderstand,attr"`
WSSE string `xml:"wsse,attr"`
SOAP string `xml:"soap,attr"`
UsernameToken struct {
XMLName xml.Name `xml:"UsernameToken"`
Username string `xml:"Username"`
Password string `xml:"Password"`
}
}
func main() {
Soap := []byte(`<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<wsse:UsernameToken>
<wsse:Username>USERNAME</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00">
<AvailStatusMessages HotelCode="HOTEL">
<AvailStatusMessage BookingLimit="10">
<StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/>
</AvailStatusMessage>
</AvailStatusMessages>
</OTA_HotelAvailNotifRQ>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>`)
res := &Envelope{}
err := xml.Unmarshal(Soap, res)
fmt.Println(res.Header.Security.UsernameToken.Username, err)
}
1、代码一
package main
import (
"fmt"
"strings"
)
var xmlRaw = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-
properties" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`
type decoder struct {
}
func main() {
fmt.Println(strings.Replace(xmlRaw, "TomHanks", "Jerry Garcia", 1))
}
2、代码二
package main
import (
"encoding/xml"
"fmt"
)
var xmlRaw = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-
properties" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`)
type decoder struct {
Keywords string `xml:"keywords"`
LastModifiedBy string `xml:"lastModifiedBy"`
//.. more xml
}
func main() {
d := decoder{}
if err := xml.Unmarshal(xmlRaw, &d); err != nil {
panic(err)
}
fmt.Println(d.LastModifiedBy)
d.LastModifiedBy = "Jerry Garcia"
bytes, err := xml.Marshal(d)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}package main
import (
"encoding/xml"
"fmt"
)
var xmlRaw = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-
properties" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`)
type decoder struct {
Keywords string `xml:"keywords"`
LastModifiedBy string `xml:"lastModifiedBy"`
//.. more xml
}
func main() {
d := decoder{}
if err := xml.Unmarshal(xmlRaw, &d); err != nil {
panic(err)
}
fmt.Println(d.LastModifiedBy)
d.LastModifiedBy = "Jerry Garcia"
bytes, err := xml.Marshal(d)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}