1

I am trying to check the format of some XML files. The following code that I tried gives Premature end of file - fatal error. The following code content describes the String formed XML detail.

    public static boolean formatXML (String content) throws SAXException, ParserConfigurationException, IOException
{
    DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder;
    builder.setErrorHandler(new SimpleErrorHandler());
    builder.parse(new InputSource(new StringReader(content)));  // Error comes here!
    return true;
}

I am looking for the following questions but they were not helpful for me:

How can I solve this problem? I already checked the format of XML files with online checker and it is good formatted.

EDIT: content inside is really a XML detail. I checked by debugging. As I worked in Linux machine I could not copied my stacktrace but made a screenshot.

enter image description here

Community
  • 1
  • 1
limonik
  • 499
  • 1
  • 6
  • 28
  • 1
    First of all I would check `content` variable, if it really contains your XML document. Can you also add stack trace of your error? I've also seen such kind of errors when XML document has external DTD or schema and it cannot be fetched due to some reasons (in my case it was proxy-server issues) – Sergi Sep 27 '16 at 09:15
  • 1
    Add `System.out.println(content)` at the start of the method to see whether the variable contains what you expect, or try using a debugger to step through it. – daiscog Sep 27 '16 at 09:22
  • @Sergi Please look my edit again – limonik Sep 27 '16 at 09:27
  • @daiscog please look my edit – limonik Sep 27 '16 at 09:28
  • Either `content` is empty, or at the start there is a problem, like a BOM char: try `content = content.replaceFirst("^\uFEFF", "");` – Joop Eggen Sep 27 '16 at 09:34
  • 1
    The parser is having a problem with the very first character (line 1, column 1). Check what the byte value is of the first character. (Perhaps it's the Unicode BOM?) Try something like: `System.out.println( String.format("%04x", (int) content.charAt(0)) )` – daiscog Sep 27 '16 at 09:37
  • @daiscog 003c. Actually I dont know what it means. – limonik Sep 27 '16 at 09:49
  • @limonik: thanks for the update, is it possible to share the XML content? – Sergi Sep 27 '16 at 10:11
  • 1
    @limonik That's the less-than sign, so should be a valid character. Are you able to step through the program using a debugger? That could show you exactly what condition is causing the exception to be thrown. – daiscog Sep 27 '16 at 10:24
  • @daiscog Exception comes this line: builder.parse(new InputSource(new StringReader(content))); – limonik Sep 27 '16 at 10:26

1 Answers1

0

Actually the reason of this error is completely different from the form of the XML. I am working on the REST API and I could send successfully Request but the device only accepts the specific formed of the Request which is different from mine. Therefore it did not send me a Response.

The solution: I changed the structure of my Request which includes XML detail.Now it works well.

limonik
  • 499
  • 1
  • 6
  • 28