xml
  • parsing
  • go
  • 2015-12-22 32 views 0 likes 
    0

    我有一個帶有以下標記的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: {{ } {{{ }} {{ } []} {{ }} {{ } {{ } }}} {{ } {{ }}}} 
    

    回答

    0

    您可以使用解碼器解組。使用解碼器,您可以關閉嚴格的解析並克服您面臨的錯誤。既然你已經把只有一行的XML/HTML解析我假設根元素和人力資源的標籤及以下之間的一些值是採樣執行

    package main 
    
    import (
        "encoding/xml" 
        "fmt" 
        "strings" 
    ) 
    
    type Hr struct { 
        XMLName xml.Name `xml:"a"` 
        TagName string `xml:"hr"` 
    } 
    
    func main() { 
        s := "<a><hr noshade>value</hr></a>" 
    
        hr := &Hr{} 
        d := xml.NewDecoder(strings.NewReader(s)) 
        d.Strict = false 
        err := d.Decode(hr) 
        if err != nil { 
         panic(err) 
        } 
    
        fmt.Println(hr.TagName) 
    } 
    

    fmt.Println(hr.TagName)將打印「值」

    +0

    謝謝!我已經添加了我迄今爲止編寫的代碼和HTML格式。雖然我沒有得到任何輸出。 – Chamila

    0

    有在你的代碼中的許多錯誤:

    • 如果屬性是不公開的,它不能被另一個軟件包(xml在這種情況下)訪問:讓所有的屬性大寫。
    • li缺少標籤名稱。

    查看運行代碼

    http://play.golang.org/p/rkNf2OfvdM

    package main 
    
    import (
        "encoding/xml" 
        "fmt" 
        "log" 
        "strings" 
    ) 
    
    type Anchor struct { 
        XMLName xml.Name `xml:"a"` 
        Href string `xml:"href,attr"` 
    } 
    
    type ListEntry struct { 
        XMLName xml.Name `xml:"li"` 
         Filename Anchor 
    } 
    
    type DirList struct { 
        XMLName xml.Name `xml:"ul"` 
        Entries []ListEntry `xml:"li"` 
    } 
    
    type Header struct { 
        XMLName xml.Name `xml:"h2"` 
    } 
    
    type Head struct { 
        XMLName xml.Name `xml:"head"` 
        Title Title 
    } 
    
    type Title struct { 
        XMLName xml.Name `xml:"title"` 
    } 
    
    type Html struct { 
        XMLName xml.Name `xml:"html"` 
        Body Body  `xml:"body"` 
        Head Head 
    } 
    
    type Body struct { 
        H2   Header 
        DirectoryList DirList 
        Hr   Hr 
        Em   Em 
    } 
    
    type Hr struct { 
        XMLName xml.Name `xml:"hr"` 
    } 
    
    type Em struct { 
        XMLName xml.Name `xml:"em"` 
        link Anchor 
    } 
    
    var contents = `<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>` 
    
    func main() { 
        htmlTag := Html{} 
        decoder := xml.NewDecoder(strings.NewReader(contents)) 
        decoder.Strict = false 
        decoder.AutoClose = xml.HTMLAutoClose 
        decoder.Entity = xml.HTMLEntity 
    
        err := decoder.Decode(&htmlTag) 
        if err != nil { 
         log.Fatal(err) 
        } 
    
        fmt.Printf("DirList: %v %#[1]v\n", htmlTag) 
    } 
    
    相關問題