How do I include &, <, > etc in XML attribute values

后端 未结 2 785
忘掉有多难
忘掉有多难 2020-11-30 12:13

I want to create an XML file which will be used to store the structure of a Java program. I am able to successfully parse the Java program and create the tags as required. T

相关标签:
2条回答
  • 2020-11-30 12:34

    You will have to escape

    " to  &quot;
    ' to  &apos;
    < to  &lt;
    > to  &gt;
    & to  &amp;
    

    for xml.

    0 讨论(0)
  • 2020-11-30 12:57

    In XML attributes you must escape

    " with &quot;
    < with &lt;
    & with &amp;
    

    if you wrap attribute values in double quotes ("), e.g.

    <MyTag attr="If a&lt;b &amp; b&lt;c then a&lt;c, it's obvious"/>
    

    meaning tag MyTag with attribute attr with text If a<b & b<c then a<c, it's obvious - note: no need to use &apos; to escape ' character.

    If you wrap attribute values in single quotes (') then you should escape these characters:

    ' with &apos;
    < with &lt;
    & with &amp;
    

    and you can write " as is. Escaping of > with &gt; in attribute text is not required, e.g. <a b=">"/> is well-formed XML.

    0 讨论(0)
提交回复
热议问题