Results 1 to 1 of 1
Thread: DTD on a classpath
- 03-29-2011, 07:26 PM #1
Member
- Join Date
- Mar 2010
- Location
- Belgrade, Serbia
- Posts
- 27
- Rep Power
- 0
DTD on a classpath
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
Similar Threads
-
Classpath
By Parneel in forum Enterprise JavaBeans (EJB)Replies: 4Last Post: 04-01-2009, 12:22 PM -
Classpath
By dawiz001 in forum New To JavaReplies: 3Last Post: 03-18-2009, 02:18 PM -
j3d classpath
By sales1 in forum New To JavaReplies: 3Last Post: 12-23-2008, 01:51 AM -
Classpath
By Preethi in forum New To JavaReplies: 5Last Post: 06-20-2008, 09:00 AM -
Classpath on mac osx
By jacobb in forum JDBCReplies: 0Last Post: 06-12-2008, 09:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks