0

I'm trying to open a xml file using java code but FileNotFoundException is thrown although file does exists. I've found the cause of the problem but just wanted suggestion how to fix it.

My Java Code is:

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

public class XMLParsing2 {
    public static void main(String args[]){

        //Get the Document Builder.
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        File file = new File("src/main/java/1.xml");
        
        System.out.println(file.getAbsolutePath());

        if (file.exists())
            System.out.println("System Actually Found it.");

        Document document = documentBuilder.parse(file);
        System.out.println("Congratulations! It's Working...");
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

And My XML Code is:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="dbtplenarprotokoll.css" type="text/css" charset="UTF-8"?>
<!DOCTYPE dbtplenarprotokoll SYSTEM "dbtplenarprotokoll.dtd">
<dbtplenarprotokoll herstellung="Satz: Satzweiss.com Print, Web, Software GmbH, Mainzer Straße             
116, 66121 Saarbrücken, www.satzweiss.com, Druck: Printsystem GmbH, Schafwäsche 1-3, 71296     
Heimsheim, www.printsystem.de" sitzung-datum="24.10.2017" sitzung-ende-uhrzeit="17:03" 
sitzung-naechste-datum="22.11.2017" sitzung-nr="1" sitzung-start-uhrzeit="11:00" start- 
seitennr="1" vertrieb="Bundesanzeiger Verlagsgesellschaft mbH, Postfach 1 0 05 34, 50445 Köln, 
Telefon (02 21) 97 66 83 40, Fax (02 21) 97 66 83 44, www.betrifft-gesetze.de" 
wahlperiode="19">
  <vorspann>
 <kopfdaten>
    <plenarprotokoll-nummer>
        Plenarprotokoll 
        <wahlperiode>
            19
        </wahlperiode>
        /<sitzungsnr>
            1
        </sitzungsnr>
    </plenarprotokoll-nummer>
    <herausgeber>
        Deutscher Bundestag
    </herausgeber>
    <berichtart>
        Stenografischer Bericht
    </berichtart>
    <sitzungstitel>
        <sitzungsnr>
            1
        </sitzungsnr>
        . Sitzung
    </sitzungstitel>
    <veranstaltungsdaten>
        <ort>
            Berlin
        </ort>, 
        <datum date="24.10.2017">
            Dienstag, den 24. Oktober 2017
        </datum>
    </veranstaltungsdaten>
 </kopfdaten>
 <inhaltsverzeichnis>
    <ivz-titel>Inhalt:</ivz-titel>
    <ivz-eintrag>
        <ivz-eintrag-inhalt>
            <redner id="11002190">
                <name>
                    <vorname>
                        Alterspräsident Dr. Hermann
                    </vorname>
                    <nachname>
                        Otto Solms
                    </nachname>
                    <rolle>
                        <rolle_lang>
                            Alterspräsident
                        </rolle_lang>
                        <rolle_kurz>
                            Alterspräsident
                        </rolle_kurz>
                    </rolle>
                </name>
            </redner>
                Alterspräsident Dr. Hermann Otto Solms
        </ivz-eintrag-inhalt>
    </ivz-eintrag>
 </inhaltsverzeichnis>
    </vorspann>
 </dbtplenarprotokoll>

And the Error is:

C:\Java\jdk-14\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community 
Edition 2019.3.3\lib\idea_rt.jar=50541:C:\Program Files\JetBrains\IntelliJ IDEA Community     
Edition 2019.3.3\bin" -Dfile.encoding=UTF-8 
@C:\Users\ABDULR~1\AppData\Local\Temp\idea_arg_file643010636 XMLParsing2
F:\Fiver Projects\Java Project\Uebung2\src\main\java\1.xml
System Actually Found it.
java.io.FileNotFoundException: F:\Fiver Projects\Java 
Project\Uebung2\src\main\java\dbtplenarprotokoll.dtd (The system cannot find the file 
specified)
   at java.base/java.io.FileInputStream.open0(Native Method)
   at java.base/java.io.FileInputStream.open(FileInputStream.java:212)
   at java.base/java.io.FileInputStream.<init>(FileInputStream.java:154)
   at java.base/java.io.FileInputStream.<init>(FileInputStream.java:109)
   at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
   at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream
   (FileURLConnection.java:184)
   at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
   at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
   at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
   at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
   at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
   at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
   at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:206)
   at XMLParsing2.main(XMLParsing2.java:35)

   Process finished with exit code 0

I'm using Intellij as IDE. Error lies in the first three lines of xml code. If I remove them Java code works just fine.

First three lines removed xml code.

<dbtplenarprotokoll herstellung="Satz: Satzweiss.com Print, Web, Software GmbH, Mainzer Straße 
116, 66121 Saarbrücken, www.satzweiss.com, Druck: Printsystem GmbH, Schafwäsche 1-3, 71296 
Heimsheim, www.printsystem.de" sitzung-datum="24.10.2017" sitzung-ende-uhrzeit="17:03" 
sitzung-naechste-datum="22.11.2017" sitzung-nr="1" sitzung-start-uhrzeit="11:00" start- 
seitennr="1" vertrieb="Bundesanzeiger Verlagsgesellschaft mbH, Postfach 1 0 05 34, 50445 
Köln, Telefon (02 21) 97 66 83 40, Fax (02 21) 97 66 83 44, www.betrifft-gesetze.de" 
wahlperiode="19">
<vorspann>
<kopfdaten>
    <plenarprotokoll-nummer>
        Plenarprotokoll 
        <wahlperiode>
            19
        </wahlperiode>
        /<sitzungsnr>
            1
        </sitzungsnr>
    </plenarprotokoll-nummer>
    <herausgeber>
        Deutscher Bundestag
    </herausgeber>
    <berichtart>
        Stenografischer Bericht
    </berichtart>
    <sitzungstitel>
        <sitzungsnr>
            1
        </sitzungsnr>
        . Sitzung
    </sitzungstitel>
    <veranstaltungsdaten>
        <ort>
            Berlin
        </ort>, 
        <datum date="24.10.2017">
            Dienstag, den 24. Oktober 2017
        </datum>
    </veranstaltungsdaten>
</kopfdaten>
<inhaltsverzeichnis>
    <ivz-titel>Inhalt:</ivz-titel>
    <ivz-eintrag>
        <ivz-eintrag-inhalt>
            <redner id="11002190">
                <name>
                    <vorname>
                        Alterspräsident Dr. Hermann
                    </vorname>
                    <nachname>
                        Otto Solms
                    </nachname>
                    <rolle>
                        <rolle_lang>
                            Alterspräsident
                        </rolle_lang>
                        <rolle_kurz>
                            Alterspräsident
                        </rolle_kurz>
                    </rolle>
                </name>
            </redner>
                Alterspräsident Dr. Hermann Otto Solms
        </ivz-eintrag-inhalt>
    </ivz-eintrag>
 </inhaltsverzeichnis>
    </vorspann>
 </dbtplenarprotokoll>

But I don't want to remove those three line. As I have many files and I don't just going to remove the xml lines from each file. Instead I want my code to be compatible enough to read and process those files.

Thanks for your suggestion in Advance.

  • The system finds your file just fine. It's complaining about `dbtplenarprotokoll.dtd` which is referenced in the DOCTYPE declaration. As described in the duplicate target answer, you need to define a local `CatalogResolver` that will resolve the DTD reference. – Jim Garrison Dec 11 '21 at 18:53

0 Answers0