Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent the Java XML Transformer using "HTML" method from adding <META> tag?

I'm using the built-in Java XML Transformer to serialize an XML document into text. I'm having a problem however when I am outputting in HTML mode.

Whenever I insert a head element the built-in transformer decides to insert a META tag with content-type data. I don't want this tag inside my data and I can't seem to find an output parameter that will disable this feature. I know I could build an xslt stylesheet and strip the tag in question, but it would be simpler just to be able to set a parameter on the transformer itself that disables it.

You might respond with "but you really should have this tag" -- trust me, I don't need it, for brevities sake I won't go into it.

Sample code

Document d; 
//d = <html><head><title></title></head><body></body></html>

Transformer t; //properly inited with no xslt specified

t.setOutputProperty(OutputKeys.METHOD,"html");
t.setOutputProperty(OutputKeys.INDENT,"no");
t.transform(new DOMSource(d), result);

System.out.println(result);

returns

<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title><style type="text/css"></style></head><body></body></html>

Case in point, I don't want that META tag there. How do I get rid of it in the easiest way possible?

UPDATE:

I've run across the option {http://xml.apache.org/xalan}omit-meta-tag which is supposed to do what I'm looking for. However it seems as though it's being ignored.

like image 893
Mike Avatar asked Sep 11 '09 05:09

Mike


2 Answers

Try added below:

trans.setOutputProperty(OutputKeys.METHOD, "xml");
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
like image 54
Sajad Avatar answered Nov 15 '22 12:11

Sajad


This is a complicated situation, as it seems the "{http://xml.apache.org/xalan}omit-meta-tag" is ignored in the built-in java transform.

So the short answer is to download something like xalanj and put it in your classpath manually.

like image 27
Mike Avatar answered Nov 15 '22 12:11

Mike