This discussion is archived
4 Replies Latest reply: Feb 18, 2013 8:27 AM by jtahlborn RSS

Special charecters handling while Converting XML string to DOM

921967 Newbie
Currently Being Moderated
Hi,

I am using the following approach for converting XML string to DOM, but due to Special characters like "&", I am getting Exceptions:

String xmlString;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xmlString)));


Can anyone please help me out on how to handle the Special characters in the above code.
  • 1. Re: Special charecters handling while Converting XML string to DOM
    gimbal2 Guru
    Currently Being Moderated
    If the XML doesn't parse, then the XML must be invalid. Show a sample of such a special character in the XML data. Please use \
     tags to post the actual XML content and any other code.                                                                                                                                                                                                                                                                                                                                                                                            
  • 2. Re: Special charecters handling while Converting XML string to DOM
    jtahlborn Expert
    Currently Being Moderated
    from your very small snippet which isn't that helpful, i'm going to guess you corrupted the xml when you converted it into a String (instead of leaving it as bytes). this is the issue 9 times out of 10 when you see someone parsing xml from a String or Reader.
  • 3. Re: Special charecters handling while Converting XML string to DOM
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Query wrote:
       Document         document = builder.parse(new InputSource(new StringReader(xmlString)));
    Maybe you need to tell your DocumentBuilder what character set the target XML should have...

    bye
    TPD
  • 4. Re: Special charecters handling while Converting XML string to DOM
    jtahlborn Expert
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    Query wrote:
       Document         document = builder.parse(new InputSource(new StringReader(xmlString)));
    Maybe you need to tell your DocumentBuilder what character set the target XML should have...
    it's too late for that. if you pass a Reader, any charset info is ignored (hence my first comment).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points