Unable to parse value containing special character? Using sax parser

前端 未结 4 1209
逝去的感伤
逝去的感伤 2021-01-13 15:48

I am new to parsing field. I\'m trying to write a parser code but unable to get the value with respect to a particular tag that value contains ampersand(&).

相关标签:
4条回答
  • 2021-01-13 16:21

    The problem is that the "&" is an escape character it self.

    To fix this you need to replace the ampersand with a unicode equivalent, i.e: "&"

    0 讨论(0)
  • 2021-01-13 16:21

    You must replace your special characters with the characters that are accepted for an XML file. In your case & should be replaced by &

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        content = String.copyValueOf(ch, start, length).trim();
        content = content.replace("&", "&")
    }
    
    0 讨论(0)
  • 2021-01-13 16:31

    You can escape these chars like html does:

    <branch_name>B &amp; P Infotech Ltd.</branch_name>
    

    Or you have use of CDATA:

    <branch_name><![CDATA[B & P Infotech Ltd.]]></branch_name>
    
    0 讨论(0)
  • 2021-01-13 16:42

    There are some characters in XML that must not appear in their literal form in an XML document, except when used as markup delimiters or within a comment, a processing instruction, or a CDATA section.
    List of characters and their corresponding entity or the numeric reference to replace :

    Original Character    XML entity replacement      XML numeric replacement
    
          "                     &quot;                       &#34;   
          <                     &lt;                         &#60;   
          >                     &gt;                         &#62;
          &                     &amp;                        &#38;
          '                     &apos;                       &#39;   
    

    you must replace above character in XML before you parse it.

    You may use CDATA Section for text that is not markup constitutes the character data of the document

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