Reputation: 60802
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.
Upvotes: 6
Views: 4593
Reputation: 60802
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.
Upvotes: 4
Reputation: 61
Try added below:
trans.setOutputProperty(OutputKeys.METHOD, "xml");
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Upvotes: 6