• Go解析soap数据和修改其中数据


    一、解析soap数据

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    二、修改其中部分数据

    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))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    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))
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    你知道SPI的原理以及实现?
    「MacOS」Swift 第三章:字符串和字符
    ArrayList 源码分析扩容原理
    Flutter应用-使用sqflite升级数据库
    高速USB转8路RS422串口
    java word转pdf、word中关键字位置插入图片 工具类
    Cmake输出git内容方式
    NLP(自然语言处理)学习记录
    Blue Prism 异常处理
    【苹果群发推】iMessage推送这是促进服务器的Apple消息
  • 原文地址:https://blog.csdn.net/qq_39852676/article/details/134471871