gpt4 book ai didi

xml - 如何在 Go 中解码包含脏 HTML 的 XML

转载 作者:数据小太阳 更新时间:2023-10-29 03:17:43 26 4
gpt4 key购买 nike

我有一些 XML 想要解码,但它在我根本不关心的字段中包含脏 HTML。我在这里发布了一个示例:http://play.golang.org/p/caKCAYyXX2

有没有办法告诉解码器跳过或忽略这些错误?我尝试制作文档中描述的非严格解码器,但无法获得 AutoCloseEntity 值的任何组合来使其正常工作。我应该提到这个 XML 来 self 无法控制的第 3 方,并且内容总是可变的,我不确定编译要跳过的静态元素列表是否可行。使用 xml:"-" 标记将 Description 添加到结构中没有任何区别。

我能够使用 Python 2.7 解析它,所以我希望它可以在 Go 中实现——尽管我更愿意将 Go 用于我的用例:)——我为此使用 Google 的 AppEngine,所以解决方案会必须在原生 Go 中,而不依赖于外部 C 库。

相关代码:

var XMLData = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<Container>
<Timestamp>2014-01-15T21:07:07.217Z</Timestamp>
<Item>
<Description>
<table width="100%" border=0 ><tr><td><table width="100%"><tr><td><!-- Begin Description -->
<TABLE cellSpacing=27 cellPadding=0 width="100%"><TBODY><TR><TD vAlign=top><P align=center>
<TABLE cellPadding=15 width="86%" border=1><TBODY><TR><TD><H3><P>
<H2><H2><H2><H2><H2><H2><H2><H2><H2><H2><H2><H2><H><H2><H2>


<IMG SRC=http://www.REMOVED.com/simage/j6x516.jpg>
<BR><BR>
<IMG SRC=http://www.REMOVED.com/simage/j6x517.jpg>

</Description>
</Item>
<Container>
</soapenv:Body>
</soapenv:Envelope>`)

type Data struct {
Timestamp string `xml:"Body>Container>Timestamp"`
}

var o Data
decoder := xml.NewDecoder(bytes.NewBuffer(XMLData))
decoder.Strict = false
decoder.AutoClose = xml.HTMLAutoClose
decoder.Entity = xml.HTMLEntity
if err := decoder.Decode(&o); err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Timestamp: ", o.Timestamp)
}

结果:错误:第 14 行的 XML 语法错误:预期元素中的/>

谢谢。

最佳答案

作为 xml 包的替代方案,如果您有 libxml2安装,你可以使用Gokogiri在 Go 中利用其解析灵 active 。

例如,使用 XPath 进行评估:

package main

import (
"fmt"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/xml"
"github.com/moovweb/gokogiri/xpath"
)

func main() {
var XMLData = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<Container>
<Timestamp>2014-01-15T21:07:07.217Z</Timestamp>
<Item>
<Description>
<table width="100%" border=0 ><tr><td><table width="100%"><tr><td><!-- Begin Description -->
<TABLE cellSpacing=27 cellPadding=0 width="100%"><TBODY><TR><TD vAlign=top><P align=center>
<TABLE cellPadding=15 width="86%" border=1><TBODY><TR><TD><H3><P>
<H2><H2><H2><H2><H2><H2><H2><H2><H2><H2><H2><H2><H><H2><H2>


<IMG SRC=http://www.REMOVED.com/simage/j6x516.jpg>
<BR><BR>
<IMG SRC=http://www.REMOVED.com/simage/j6x517.jpg>

</Description>
</Item>
<Container>
</soapenv:Body>
</soapenv:Envelope>`)

doc, err := gokogiri.ParseXml(XMLData)

if err != nil {
fmt.Printf("XML document could not be parsed")
return
}

nxpath := xpath.NewXPath(doc.DocPtr())
nodes, err := nxpath.Evaluate(doc.DocPtr(), xpath.Compile("//Timestamp"))

if err != nil {
fmt.Printf("XPath could not be evaluated")
return
}

if len(nodes) == 0 {
fmt.Printf("Elements matching XPath not found")
return

}
timestamp := xml.NewNode(nodes[0], doc).InnerHtml()

fmt.Printf("%s", timestamp) // "2014-01-15T21:07:07.217Z"
}

这适用于 OS X 10.9.1 上的 Go v1.2。 Gokogiri 包还包括一个 CSS 选择器转换器,但我从未使用过它,也不能保证它。

关于xml - 如何在 Go 中解码包含脏 HTML 的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21322093/

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