我有一個帶有以下標記的html輸出。轉到XML - 解析HTML中的布爾屬性導致XML驗證錯誤
<hr noshade>
我對這個結構是
type Hr struct {
TagName xml.Name `xml:"hr"`
}
當我嘗試使用「編碼/ XML」傳遞HTML,它拋出一個錯誤說屬性沒有'='
字符。
我看到這個錯誤是由於默認的解碼器評估XML爲Strict
設置爲true而引發的。
如何忽略此操作並繼續解析文檔(使用xml.Unmarshal()
)?
編輯:包括XML和使用的結構。
我找到了解碼器的設置,並使用了NewDecoder,但是看起來unmarshalling沒有正確發生。
<html><head><title>Some title</title></head>
<body>
<h2>Title here</h2>
<ul>
<li><a href="../">..</a></li>
<li><a href="file1.txt">file1.txt</a></li>
<li><a href="file2.zip">file2.zip</a></li>
.....
</ul>
<hr noshade><em>Powered by <a href="http://subversion.apache.org/">Apache Subversion</a> version 1.7.18 (r1615261).</em>
</body></html>
代碼我已經寫到目前爲止
type Anchor struct {
TagName xml.Name `xml:"a"`
Href string `xml:"href,attr"`
}
type ListEntry struct {
TagName xml.Name `xml:"li"`
Filename Anchor
}
type DirList struct {
XMLName xml.Name `xml:"ul"`
Entries []ListEntry
}
type Header struct {
TagName xml.Name `xml:"h2"`
}
type Head struct {
TagName xml.Name `xml:"head"`
title Title
}
type Title struct {
TagName xml.Name `xml:"title"`
}
type html struct {
TagName xml.Name `xml:"html"`
body Body `xml:"body"`
head Head
}
type Body struct {
H2 Header
DirectoryList DirList
hr Hr
em Em
}
type Hr struct {
TagName xml.Name `xml:"hr"`
}
type Em struct {
TagName xml.Name `xml:"em"`
link Anchor
}
contents := retrieveFromWeb()
htmlTag := html{}
decoder := xml.NewDecoder(strings.NewReader(contents))
decoder.Strict = false
decoder.AutoClose = xml.HTMLAutoClose
decoder.Entity = xml.HTMLEntity
err = decoder.Decode(&htmlTag)
fmt.Println("DirList: ", htmlTag)
電流輸出
DirList: {{ } {{{ }} {{ } []} {{ }} {{ } {{ } }}} {{ } {{ }}}}
謝謝!我已經添加了我迄今爲止編寫的代碼和HTML格式。雖然我沒有得到任何輸出。 – Chamila