How do I keep the html tags when parsing xml?

user_78361084 :

I have the following xml I am trying to parse. My playground can be found here

package main

import "fmt"
import "encoding/xml"

type ResultSlice struct {
    MyText []Result `xml:"results>result"`
}
type Result struct {
    MyResult string `xml:"text"`
}

func main() {
    s := `<myroot>
            <results>             
              <result><text><strong>This has style</strong>Then some not-style</text></result>
              <result><text>No style here</text></result>
              <result><text>Again, no style</text></result>
        </results>
          </myroot>`
    r := &ResultSlice{}
    if err := xml.Unmarshal([]byte(s), r); err == nil {
        fmt.Println(r)
    } else {
        fmt.Println(err)
    }

}

This will only print out the plain text and anything within html tags gets ignored. <strong>This has style</strong> gets ignored. How do I include that as well?

thx!

Ainar-G :

Use innerxml tag:

type ResultSlice struct {
    MyText []Result `xml:"results>result"`
}

type Result struct {
    Text struct {
        HTML string `xml:",innerxml"`
    } `xml:"text"`
}

Playground: http://play.golang.org/p/U8SIUIvOC_

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I extract the full html from xml (including tags)?

Losing spaces when parsing XML with HTML tags to CSV

In spaCy, how do I keep proper nouns together when parsing a sentence?

How can I check the existence of attributes and tags in XML before parsing?

How can I check the existence of attributes and tags in XML before parsing?

How do I set IntelliJ to place xml tags on separate lines when formatting xml files?

How to keep html tags when writing a ElementTree tree to disk?

How can I remove selected text and keep empty HTML tags?

How do I turn off validation when parsing well-formed XML using DocumentBuilder.parse?

How do I add ending tags (html)

Parsing HTML tags from inside XML in PHP

Groovy : parsing xml with HTML tags inside

Difficulties with internal tags when parsing xml

How to do XML parsing?

PHP: xml_parser "Mismatched tag"-error when parsing HTML (auto-closing tags as <img>)?

Why do I keep getting an error when deserializing XML collection?

How do I keep the CefSharp browser from open 2 windows when following an html link?

How do I keep behavior and content separate when inserting HTML via jQuery?

How do I keep a secondary GET variable, when updating the form in HTML

Parsing XML with ElementTree's iter() isn't finding my tags when I pass a specific argument

BS4: How would I remove unncessary html tags and only keep the <p> and <ruby> tags?

How do I pass a url to NSBundle for xml parsing with AEXML?

How do I stop parsing an XML document with IVBSAXXMLReader in Delphi?

How do I optimize this XML parsing loop for speed?

How can I read html tags from within an xml file?

How do I comment out a block of tags in XML?

How do I parse an XML file for certain tags in Python?

How can I check the existence of tags in XML before parsing using ElementTree?

How do I add an HTML page for template tags when I am using Routes to display web app pages

TOP Ranking

HotTag

Archive