How do I escape ampersands in XML so they are rendered as entities in HTML?

前端 未结 11 1707
野趣味
野趣味 2020-11-22 15:59

I have some XML text that I wish to render in an HTML page. This text contains an ampersand, which I want to render in its entity representation: &.

相关标签:
11条回答
  • 2020-11-22 16:38

    When your XML contains &, this will result in the text &.

    When you use that in HTML, that will be rendered as &.

    0 讨论(0)
  • 2020-11-22 16:39

    How about using the unicode \u0026? Works for me in my android XML files. If problems arise, someone let me know.

    0 讨论(0)
  • 2020-11-22 16:42

    I have tried &amp, but it didn't work. Based on Wim ten Brink's answer I tried &amp and it worked.

    One of my fellow developers suggested me to use & and that worked regardless of how many times it may be rendered.

    0 讨论(0)
  • 2020-11-22 16:47

    The & character is itself an escape character in XML so the solution is to concatenate it and a Unicode decimal equivalent for & thus ensuring that there are no XML parsing errors. That is, replace the character & with &.

    0 讨论(0)
  • 2020-11-22 16:52

    Consider if your XML looks like below.

    <Employees Id="1" Name="ABC">
      <Query>
        SELECT * FROM EMP WHERE ID=1 AND RES<>'GCF'
      <Query>
    </Employees>
    

    You cannot use the <> directly as it throws an error. In that case, you can use &#60;&#62; in replacement of that.

    <Employees Id="1" Name="ABC">
      <Query>
        SELECT * FROM EMP WHERE ID=1 AND RES &#60;&#62; 'GCF'
      <Query>
    </Employees>
    

    Click here to see all the codes.

    0 讨论(0)
  • 2020-11-22 16:57

    Use CDATA tags:

     <![CDATA[
       This is some text with ampersands & other funny characters. >>
     ]]>
    
    0 讨论(0)
提交回复
热议问题