当前位置: 首页 > news >正文

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、代码一

package mainimport ("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 mainimport ("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 mainimport ("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))}
http://www.lryc.cn/news/233678.html

相关文章:

  • LeetCode98. Validate Binary Search Tree
  • 【LeetCode】206. 反转链表
  • 飞天使-通过GET 和POST进案例演示
  • 【MySql】12- 实践篇(十)
  • <C++> 反向迭代器
  • 【EI会议征稿】第三届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2024)
  • 格力报案称“高管遭自媒体侮辱诽谤”
  • HBase之Compaction
  • 设计模式之结构型模式
  • centOs 6.10 编译 qt 5.15.11
  • Redis对象的数据结构及其原理汇总
  • @RestController 注解网页返回 [] ,出现的bug
  • C语言指针详解(1)(能看懂字就能明白系列)文章超长,慢慢品尝
  • 为什么别人年薪30W+?同样为测试人,“我“的测试之路...
  • 【Unity】XML文件的解析和生成
  • Vue h5页面手指滑动图片
  • Python类属性下划线的意义
  • DbUtils概述
  • 大数据基础设施搭建 - Hadoop
  • 测试开发环境下centos7.9下安装docker的minio
  • Django之模版层
  • spark性能调优 | 内存优化
  • 【PG】PostgreSQL高可用之自动故障转移-repmgrd
  • 操作系统OS/存储管理/内存管理/内存管理的主要功能_基本原理_要求
  • 【手写数据库toadb】SQL解析器的实现架构,create table/insert 多values语句的解析树生成流程和输出结构分析
  • 设计模式-备忘录模式-笔记
  • 机器学习—基本术语
  • pytorch单精度、半精度、混合精度、单卡、多卡(DP / DDP)、FSDP、DeepSpeed模型训练
  • 基于PHP的纺织用品商城系统
  • Go使用命令行输出二维码