惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
WordPress大学
WordPress大学
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
P
Proofpoint News Feed
D
DataBreaches.Net

希仁之拥

领克900半年使用体验 | 希仁之拥的博客 Ubuntu 26.04 Desktop使用体验 | 希仁之拥的博客 【转载】谈谈不受欢迎的博客技术特征 | 希仁之拥的博客 【转载】ClaudeCode 你想知道的所有秘密,源码深度研究报告 | 希仁之拥的博客 2025年年终总结 | 希仁之拥的博客 集成和使用Openclaw后的思考 | 希仁之拥的博客 我买了领克900 | 希仁之拥的博客 服务器性能优化之io拷贝 | 希仁之拥的博客 Go-Sail导航站上线啦 | 希仁之拥的博客 今年国庆的一些感受 [2025] | 希仁之拥的博客 在Deepin 25上配置forticlient | 希仁之拥的博客 分享一些酷酷的站点 [20250908] | 希仁之拥的博客 Go-Sail发布v3.0.6版本了 | 希仁之拥的博客 我对V2EX发布$V2EX讨论的一些感受 | 希仁之拥的博客 如何让Stripe支持支付宝和微信支付 | 希仁之拥的博客 2025上半年里程碑 | 希仁之拥的博客 GitLab+Drone使用体验 | 希仁之拥的博客 四姑娘山之旅 | 希仁之拥的博客 近来帮同事做性能优化的过程回顾 | 希仁之拥的博客 聊聊接口的返回数据结构 | 希仁之拥的博客 由GORM的Updates语法糖 我把 Go-Sail 的文档站更新了 | 希仁之拥的博客 这就是我为什么讨厌拼多多 | 希仁之拥的博客 元旦快乐~ | 希仁之拥的博客 致敬还在写博客的我们 | 希仁之拥的博客 逐步的把图片资源迁移到星光图床上 | 希仁之拥的博客 帮弟弟配了一台mini主机 | 希仁之拥的博客 国庆的一些碎碎念 | 希仁之拥的博客 就这一刻而言,我觉得科技冷冰冰的。 | 希仁之拥的博客 如何使用acme.sh自动续签证书 | 希仁之拥的博客
golang解析带命名空间的xml数据(带冒号的节点或属性名) | 希...
希仁之拥 · 2020-12-07 · via 希仁之拥

XML数据结构

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Zeebe Modeler" exporterVersion="0.1.0">
  <bpmn:process id="order-process" isExecutable="true">
    <bpmn:startEvent id="order-placed" name="Order Placed">
      <bpmn:outgoing>SequenceFlow_18tqka5</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:endEvent id="order-delivered" name="Order Delivered">
      <bpmn:incoming>SequenceFlow_1qj94z0</bpmn:incoming>
    </bpmn:endEvent>
  </bpmn:process>
</bpmn:definitions>

解析(反序列化)

  • 定义结构体
type bp struct {
	XMLName xml.Name `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL definitions"`
	// <!--attributes start-->
	BPMN            string `xml:"bpmn,attr"`
	BPMNDI          string `xml:"bpmndi,attr"`
	DI              string `xml:"di,attr"`
	DC              string `xml:"dc,attr"`
	Zeebe           string `xml:"zeebe,attr"`
	XSI             string `xml:"xsi,attr"`
	ID              string `xml:"id,attr"`
	TargetNamespace string `xml:"targetNamespace,attr"`
	Exporter        string `xml:"exporter,attr"`
	ExporterVersion string `xml:"exporterVersion,attr"`
	// <!--attributes end-->
	Process struct {
		// <!--attributes start-->
		ID           string `xml:"id,attr"`
		Name         string `xml:"name,attr"`
		IsExecutable bool   `xml:"isExecutable,attr"`
		// <!--attributes end-->
		StartEvent struct {
			// <!--attributes start-->
			ID   string `xml:"id,attr"`
			Name string `xml:"name,attr"`
			// <!--attributes end-->
			Outgoing string `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL outgoing"`
		} `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL startEvent"`
		EndEvent struct {
			// <!--attributes start-->
			ID   string `xml:"id,attr"`
			Name string `xml:"name,attr"`
			// <!--attributes end-->
			Incoming string `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL incoming"`
		} `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL endEvent"`
	} `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL process"`
}
  • 解析
var target bp
err = xml.Unmarshal(bpmn, &target)
if err != nil {
	fmt.Println("parse xml file error: ", err)
}
fmt.Println("Print xml struct:")
fmt.Println(target, target.BPMNDI, target.ExporterVersion)
fmt.Println(target.Process.IsExecutable)
fmt.Println(target.Process.StartEvent.Outgoing)

序列化

值得注意的是,直接使用上面的bp结构体序列化为xml字符串会发现结果的结构与预期并不一致。golang playground

相关issue #9519

解决方案

解决方案是将序列化(Marshal)和反序列化(Unmarshal)的结构体分别定义。golang playground

  • 定义结构体(仅修改了部分)
type bpEn struct {
	XMLName xml.Name `xml:"bpmn:definitions"` // <----- 修改
	// <!--attributes start-->
	BPMN            string `xml:"xmlns:bpmn,attr"` // <---- 修改
	BPMNDI          string `xml:"bpmndi,attr"`
	DI              string `xml:"di,attr"`
	DC              string `xml:"dc,attr"`
	Zeebe           string `xml:"zeebe,attr"`
	XSI             string `xml:"xsi,attr"`
	ID              string `xml:"id,attr"`
	TargetNamespace string `xml:"targetNamespace,attr"`
	Exporter        string `xml:"exporter,attr"`
	ExporterVersion string `xml:"exporterVersion,attr"`
	// <!--attributes end-->
	Process struct {
		// <!--attributes start-->
		ID           string `xml:"id,attr"`
		Name         string `xml:"name,attr"`
		IsExecutable bool   `xml:"isExecutable,attr"`
		// <!--attributes end-->
		StartEvent struct {
			// <!--attributes start-->
			ID   string `xml:"id,attr"`
			Name string `xml:"name,attr"`
			// <!--attributes end-->
			Outgoing string `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL outgoing"`
		} `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL startEvent"`
		EndEvent struct {
			// <!--attributes start-->
			ID   string `xml:"id,attr"`
			Name string `xml:"name,attr"`
			// <!--attributes end-->
			Incoming string `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL incoming"`
		} `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL endEvent"`
	} `xml:"http://www.omg.org/spec/BPMN/20100524/MODEL process"`
}
  • 序列化
	bpmn := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Zeebe Modeler" exporterVersion="0.1.0">
  <bpmn:process id="order-process" isExecutable="true">
    <bpmn:startEvent id="order-placed" name="Order Placed">
      <bpmn:outgoing>SequenceFlow_18tqka5</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:endEvent id="order-delivered" name="Order Delivered">
      <bpmn:incoming>SequenceFlow_1qj94z0</bpmn:incoming>
    </bpmn:endEvent>
  </bpmn:process>
</bpmn:definitions>`)

	var en bpEn
	en.Process.IsExecutable = false
	en.Process.ID = "identity"
	en.Process.Name = "Hello Someone"
	en.Process.StartEvent.Outgoing = "xyz"
	enSt, err := xml.MarshalIndent(&en, "", "	")
	fmt.Println("原本结构:\n", string(bpmn), "\n", "序列化后结构:\n", string(enSt), err)
	return

参考文章:

https://blog.csdn.net/qq_24210767/article/details/102829205

https://studygolang.com/articles/5328

转载请注明原文地址:https://blog.keepchen.com/a/golang-parse-xml-with-namespace-prefix.html