I finished an application that among other things let user save work to an xml file and let load previous work from an xml file.


I use this dtd file for validating

Java Code:
<!ELEMENT JavaTreeXML (Node)>
<!ELEMENT Node (Title, Text, Node*)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Text (#PCDATA)>

and this is the example of the xml file.

Java Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE JavaTreeXML SYSTEM "javatree.dtd">
<JavaTreeXML>
<Node>
<Title>main</Title>
<Text>etjhejed</Text>
<Node>
<Title>1</Title>
<Text>rwhsw
hgkfrk</Text>
<Node>
<Title>11</Title>
<Text>egjwaehgsal</Text>
</Node>
</Node>
<Node>
<Title>2</Title>
<Text>tdjdeju
yr
tuezt5</Text>
</Node>
</Node>
</JavaTreeXML>

While dtd file is in the same directory as xml file everything work fine. But when they are not in the same directory application throws an exception. Below is the code of methods that do the loading and saving.

Java Code:
	/**
	 * Save document to file and return true is successful. Return false if error occurs.
	 * 
	 * @param document
	 * @param file
	 * 
	 * @return status
	 */
	private boolean saveDocumentToFile(Document document, File file){
		
		boolean status = true;
		
		try
		{
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			
			transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "javatree.dtd");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.METHOD, "xml");
			
			transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream(file)));
		}
		catch (Exception e)
		{
			status = false;
			e.printStackTrace();
		}
		
		return status;
	}
	
	/**
	 * Reads a file and creates a document. Returns document if successful, or null if error occurs.
	 * 
	 * @param file
	 * 
	 * @return document
	 */
	private Document loadDocumentFromFile(File file){
		
		Document document = null;
		
		try
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(true);
            factory.setIgnoringElementContentWhitespace(true);
            
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setEntityResolver(new EntityResolver() {
                @Override
                public InputSource resolveEntity(String publicId, String systemId)
                        throws SAXException, IOException {
                    if (systemId.contains("schema.dtd")) {
                        return new InputSource(getClass().getResourceAsStream("javatree.dtd"));
                    } else {
                        return null;
                    }
                }
            });

                        
            document = builder.parse(file);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            document = null;
        }

        return document;
	}

Original loading was done with out entity resolver. In both versions code throws an exception when dtd file is not in the same directory as the xml file. I would like to keep dtd file in the jar file that contains the whole application. Could you please tell me how can I do that.

Thanks in advance