How can we parse DOCTYPE information using XMLEventReader?

I have some existing code that parses the namespace of the top-level elements to determine which XML file we are looking for.

XMLEventReader reader = createXMLEventReader(...); try { while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.DTD: // No particularly useful information here? //((DTD) event).getDocumentTypeDeclaraion(); break; case XMLStreamConstants.START_ELEMENT: formatInfo.qName = ((StartElement) event).getName(); return formatInfo; default: break; } } } finally { reader.close(); } 

If I allow the parser to download DTDs from the Internet, getDocumentTypeDeclaraion() contains a giant string with more information than I know how to deal with it, because it inserts all the associated DTDs into the string before passing it. On the other hand, if I block the parser downloading the DTD from the Internet (which is preferable anyway, for obvious reasons), it gives me the line, "<!DOCTYPE" .

Is there no way to return values ​​inside a DOCTYPE?

I use the default parser that comes with the JRE, in case that matters.

+4
source share
1 answer

I know this old post, but I could not find the answer on the Web until I found your question, which pointed me in the right direction.

Here, the external unparsed objects for the DTD are retrieved by including the value specified by the XMLEvent#getEventType() method.

 XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setXMLResolver(new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { //return a closed input stream if external entities are not needed return new InputStream() { @Override public int read() throws IOException { return -1; } }; } }); XMLEventReader reader = factory.createXMLEventReader( . . . ); try { while(reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.DTD: List<EntityDeclaration> entities = ((DTD)event).getEntities(); if (entities != null) { for (EntityDeclaration entity : entities) System.out.println(entity.getName() + " = " + entity.getSystemId()); } break; case . . . } } } finally { reader.close(); } 
+2
source

All Articles