0

I want to extract a rar file with junrar library. When I extract an xml file as InputStream, dom4j throws an exception:

org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.

How do I solve this?

 String filename = importFile.getAbsolutePath();
        File f = new File(filename);
        Archive a = null;
        try {
            a = new Archive(f);
        } catch (RarException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (a != null) {
            a.getMainHeader().print();
            FileHeader fh = a.nextFileHeader();
            while (fh != null) {
                try {
                                    if(fh.isDirectory()){
                                        continue;
                                    }
                                    else if(fh.getFileNameString().endsWith(".jml")||fh.getFileNameString().endsWith(".xml")){

                                        InputStream in=a.getInputStream(fh);
;
                                        List<Card> xml = convertXml(in);
                                        if(xml!=null){
                                         xmlPath = createNewXml(xml,fh.getFileNameString());
                                        }
                                        ZipEntry newZe=new ZipEntry(fh.getFileNameString().substring(0,fh.getFileNameString().lastIndexOf("."))+"xml");            
                                        zos.putNextEntry(newZe);
                                        InputStream inXml = new FileInputStream(xmlPath);
                                           int len;
                                           while ((len = inXml.read(buffer))>0 ) {
                                               zos.write(buffer, 0, len);
                                           }
                                           in.close();
                                           zos.closeEntry();
                                        }
                                    else if(fh.getFileNameString().startsWith("images")){
                                         ZipEntry newZe=new ZipEntry(fh.getFileNameString());
                                         zos.putNextEntry(newZe);
                                           InputStream inFile=a.getInputStream(fh);
                                            int len;
                                              while ((len = inFile.read(buffer))>0 ) {
                                                zos.write(buffer, 0, len);
                                            }

                                            inFile.close();
                                            zos.closeEntry();

                                    }
                                }catch(RarException | IOException | DocumentException e){
                                    e.printStackTrace();
                                }
                fh = a.nextFileHeader();
            }

        }

                 zos.close();
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Possible duplicate of [Content is not allowed in Prolog SAXParserException](https://stackoverflow.com/questions/4569123/content-is-not-allowed-in-prolog-saxparserexception) – John Scattergood Jul 16 '17 at 19:50
  • BTW `+ ".xml"`. The way to solve such problems, is to read the actual bytes and inspect the "XML" text, instead of processing the InputStream as XML. `Files.copy(in, Paths.get("xml.txt"));` – Joop Eggen Jul 16 '17 at 20:28

0 Answers0