0

I get an error when trying to parse a XML file with escape characters in it using the following java code. Is there a way to handle it while parsing the file here?

private Document parseXmlFile(String fileName) {
            Document doc = null;
            try {
                File fXmlFile = new File(fileName);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();
            } catch (ParserConfigurationException | SAXException | IOException ex) {
                System.out.println(ex);
            }
            return doc;

        }

Sample XML

<ResultDetail>
        <ObjectType>APF</ObjectType>
        <ObjectName>dlgCreateNewEmployee</ObjectName>
        <Header>AccessModifiers Detected</Header>
        <Description>&Original source: private</Description>
</ResultDetail>
wishman
  • 774
  • 4
  • 14
  • 31
  • 2
    That is invalid xml. The text should be wrapped in a CDATA block. Or encode the ``&`` as ``&``. – f1sh Jan 03 '17 at 08:42
  • @f1sh Is there a way to handle such invalid XML files? Cause in this scenarios, such invalid XML files are still parsed! – wishman Jan 03 '17 at 08:47
  • 1
    Possible duplicate of [I need to parse non well-formed xml data (HTML)](http://stackoverflow.com/questions/2560783/i-need-to-parse-non-well-formed-xml-data-html) – Joe Jan 03 '17 at 08:47

1 Answers1

0
   <ResultDetail>
    <ObjectType>APF</ObjectType>
    <ObjectName>dlgCreateNewEmployee</ObjectName>
    <Header>AccessModifiers Detected</Header>
    <Description>&#038;Original source: private</Description>

Alternatively you can use CDATA also

Community
  • 1
  • 1
karthik
  • 17
  • 2