2017-11-30 02:45



For example, if the xml is simple:

<p>this is a test xml</p>

Then I can use struct like this:

type Dummy struct { XMLName xml.Name `xml:"p"` Value string `xml:",chardata"` }

But if the xml has special character, like

<p>this is a test < xml & </p>

I used the same struct and it caused error, and it seems nothing to do with struct, because I use interface{} and it caused error the same.

I read the document of package "encoding/xml" but cannot find the right way.

Does anyone know how to do this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongrong6235 dongrong6235 4年前

    This XML is not well-formed. It contain syntax error because character & has a special meaning. If you need to insert if as a symbol itself change it with &amp;. After parsing it will be replaced back to usual &.

    The same rule applies to < - it should be replaced with &lt;.

      <p>this is a test <gt; xml &amp; </p>

    This is the general rule for all XML parsers and encoders. Any other XML system awaits similar behavior.

    点赞 评论 复制链接分享
  • dsfd3546 dsfd3546 4年前

    If you look at the error (try it on golang playground), it's telling you the input is not valid XML.

    XML syntax error on line 1: expected element name after <

    The text is not allowed to contain plain < or & like that, see the Character data section of the xml spec

    点赞 评论 复制链接分享