Problemas no Doctype do XML!

4 respostas
A

Tenho esse método abaixo, onde estou tentando gerar um XHTML, porém nem por Deus consigo colocar o Doctype. Reparem que o ID publico e privado do Doctype é setado porém não aparece quando mando escrever o XHTML. Alguém pode me ajudar???

public void write(Writer writer) throws IOException {
	try {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		// Create a XML document
		Document docXml = builder.newDocument();

		// Fill XML document with XHTML tags
		document.createXml(docXml);

		// Format
		OutputFormat format = new OutputFormat();
		format.setMethod(Method.XHTML);
		format.setOmitXMLDeclaration(true);
		format.setDoctype(document.getDoctype().getPubId(), document.getDoctype().getSysId());
		format.setIndenting(indenting);

		// Write XHTML
		XMLSerializer s = new XMLSerializer(writer, format);
		s.serialize(docXml);

		writer.flush();
		writer.close();
	} catch (ParserConfigurationException e) {
	}
}

4 Respostas

A

Ninguem pessoal?

victorwss

É. Parece que ninguém.
A gambiarra que você pode tentar é escrever ele manualmente no Writer antes de começar a serialização do XML.

A

É. Parece que ninguém.
A gambiarra que você pode tentar é escrever ele manualmente no Writer antes de começar a serialização do XML.

Quero tentar resolver o problema de maneira elegante antes de partir pra POG! :smiley:

A

Bom pessoal depois de muito procurar finalmente resolvi meu problema. Para resolver defini o doctype no momento de criar o Document. Portanto o método ficou assim:

public void write(Writer writer) throws IOException, ParserConfigurationException, SAXException {
		// Creates Factory
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setValidating(validate);

		// Creates Document Builder
		DocumentBuilder builder = factory.newDocumentBuilder();
		DOMImplementation domimpl = builder.getDOMImplementation();

		// Create a XML document
		DocumentType doctype = domimpl.createDocumentType("html", document.getDoctype().getPubId(), document.getDoctype().getSysId());
		Document docxml = domimpl.createDocument("http://www.w3.org/1999/xhtml", "html", doctype);

		// Fill XML document with XHTML tags
		document.createXml(docxml);

		// Format
		OutputFormat format = new OutputFormat();
		format.setMethod(Method.XHTML);
		format.setOmitXMLDeclaration(true);
		format.setIndenting(indenting);

		// Write XHTML
		XMLSerializer s = new XMLSerializer(writer, format);
		s.serialize(docxml);

		writer.flush();
		writer.close();
	}

Reparem nas linhas:

DocumentType doctype = domimpl.createDocumentType("html", document.getDoctype().getPubId(), document.getDoctype().getSysId());
		Document docxml = domimpl.createDocument("http://www.w3.org/1999/xhtml", "html", doctype);

É isso ai pessoal!!! Resolvido! :smiley:

Criado 23 de janeiro de 2009
Ultima resposta 26 de jan. de 2009
Respostas 4
Participantes 2