gpt4 book ai didi

go - XML 解码不遵守根元素 namespace 前缀定义

转载 作者:IT王子 更新时间:2023-10-29 01:21:45 24 4
gpt4 key购买 nike

这是 XML 结构:

<root xmlns:test="http://test.com/testns">
<test:sub>
<title>this is title</title>
</test:sub>
</root>

它用下面定义的结构解码:

type Root struct {
XMLName xml.Name `xml:"root"`
Sub *Sub
}

type Sub struct {
XMLName xml.Name `xml:"http://test.com/testns sub"`
Title string `xml:"title"`
}

这是被编码回来的:

<root>
<sub xmlns="http://test.com/testns">
<title>this is title</title>
</sub>
</root>

根命名空间前缀定义在编码之后被删除,子元素使用 url 命名空间而不是前缀。这是 code

编码/解码有什么方法不会改变 xml 结构吗?谢谢!

最佳答案

看起来并没有改变逻辑结构。在您的原始输入中,root 元素为命名空间 http://test.com/testns 声明了前缀 test 但它没有实际上声明自己在那个命名空间中。

这是一个替代版本,它看起来像你想要的那样:https://play.golang.org/p/NqNyIyMB4IP

我将命名空间提升到 Root 结构,并将 test: 前缀添加到输入中的 root xml 元素。

关于go - XML 解码不遵守根元素 namespace 前缀定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32705546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com