In an ideal world, you'd be able to validate using a Validator . Something like this.
Up vote 13 down vote favorite 4 share g+ share fb share tw.
The XML file does not have any DOCTYPE declaration (or may have one that should then be overridden). I had a look at this thread but besides the fact they are using . NET I doubt that this is a good solution.
Any input appreciated! Java xml validation dtd link|improve this question asked Jul 8 '09 at 6:17Simon583716 100% accept rate.
In an ideal world, you'd be able to validate using a Validator. Something like this: SchemaFactory schemaFactory = SchemaFactory . NewInstance(XMLConstants.
XML_DTD_NS_URI); Schema schema = schemaFactory. NewSchema(new File( "xmlValidate. Dtd")); Validator validator = schema.newValidator(); validator.
Validate(new StreamSource("xmlValidate. Xml")); Unfortunately, the Sun implementation (at least, as of Java 6) does not include support for creating a Schema instance from a DTD. You might be able to track down a 3rd party implementation.
Your best bet may be to alter the document to include the DTD before parsing using some other mechanism. You can use a transformer to insert a DTD declaration: TransformerFactory tf = TransformerFactory .newInstance(); Transformer transformer = tf.newTransformer(); transformer. SetOutputProperty( OutputKeys.
DOCTYPE_SYSTEM, "xmlValidate. Dtd"); transformer. Transform(new StreamSource( "xmlValidate.
Xml"), new StreamResult(System. Out)); ...but this does not seem to replace an existing DTD declaration. This StAX event reader can do the job: public static class DTDReplacer extends EventReaderDelegate { private final XMLEvent dtd; private boolean sendDtd = false; public DTDReplacer(XMLEventReader reader, XMLEvent dtd) { super(reader); if (dtd.getEventType()!
= XMLEvent. DTD) { throw new IllegalArgumentException("" + dtd); } this. Dtd = dtd; } @Override public XMLEvent nextEvent() throws XMLStreamException { if (sendDtd) { sendDtd = false; return dtd; } XMLEvent evt = super.nextEvent(); if (evt.getEventType() == XMLEvent.
START_DOCUMENT) { sendDtd = true; } else if (evt.getEventType() == XMLEvent. DTD) { // discard old DTD return super.nextEvent(); } return evt; } } It will send a given DTD declaration right after the document start and discard any from the old document. Demo usage: XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent dtd = eventFactory .
CreateDTD(""); XMLInputFactory inFactory = XMLInputFactory.newInstance(); XMLOutputFactory outFactory = XMLOutputFactory.newInstance(); XMLEventReader reader = inFactory . CreateXMLEventReader(new StreamSource( "xmlValidate. Xml")); reader = new DTDReplacer(reader, dtd); XMLEventWriter writer = outFactory.
CreateXMLEventWriter(System. Out); writer. Add(reader); writer.flush(); // TODO error and proper stream handling Note that the XMLEventReader could form the source for some other transformation mechanism that performed validation.
It would be much easier to validate using a W3 schema if you have that option.
Thank you very much for your extensive answer, that really helps me a lot. I will have a look into converting the DTD to a W3 schema, as I can use the Validator of Sun then. – Simon Jul 8 '09 at 23:20 my XML file does not have any DOCTYPE declaration.
And I'm parsing file using SAXParser in Android. DTD generated by myself. How can I validate an XML file using my DTD, in SAX parsing?
– Khushbu Nov 12 '11 at 11:52 @Khushbu - it would be better if you asked a new question. – McDowell Nov 12 '11 at 17:11 I have already asked question but I have not get proper answer. – Khushbu Nov 15 '11 at 3:54.
You have to implement the EntityResolver, checkout this example.
– J-16 SDiZ Jul 8 '09 at 7:12 Against my own DTD. I just want to make sure the XML I receive conforms to my DTD, not just any DTD the sender specifies. – Simon Jul 8 '09 at 23:09.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.