| Author |
Removing encoding attribute from the <XML> element
|
Srikanth Madasu
Ranch Hand
Joined: Sep 10, 2008
Posts: 48
|
|
I am creating an XML file that will be used on the mainframe machine. But I have some issues with the generated XML file.
I am using the DOM object and XMLWriter class to generate the file. I am using following code
But this generates an XML file that looks like below :
<?xml version="1.0" encoding="UTF-8"?>
<root><doc><info><docDescription> blah blah.... </root>
But I want to remove the encoding="UTF-8" attribute and want the entire XML without any spaces/new line characters like below...
<?xml version="1.0"?><root><doc><info><docDescription> blah blah.... </root>
Does Anyone know how to achieve this???
Thanks in advance.....
|
If it's easy.. then everybody would do it!
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
|
|
I suppose you could write your own custom OutputStream to wrap FileOutputStream, substitute the <?xml version="1.0"> you want and then simply discard the formatting spaces, tabs, crlfs etc.
There are only a few methods in java.io.OutputStream that you would have to customize.
Bill
|
Java Resources at www.wbrogden.com
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
I don't see an XmlWriter class in the standard Java API. (I see an XmlWriter interface but it doesn't match what's in your code.) So is this somebody else's XmlWriter? Somebody else with a forum or a mailing list?
|
 |
Srikanth Madasu
Ranch Hand
Joined: Sep 10, 2008
Posts: 48
|
|
Its a class from org.dom4j.io.* package...
documentation here
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Ah, I see. So the documentation answers one of your questions when it says
The XML declaration and processing instructions are always on their own lines.
And it appears to answer the rest of your questions when it says
An OutputFormat object can be used to define how whitespace is handled when printing and allows various configuration options, such as to allow suppression of the XML declaration, the encoding declaration or whether empty documents are collapsed.
So apparently you need to do something different with the OutputFormat you are creating. I assume you're going through this exercise because the mainframe people have an XML parser which makes unreasonable demands about its input?
|
 |
Srikanth Madasu
Ranch Hand
Joined: Sep 10, 2008
Posts: 48
|
|
Paul, thanks for your reply... finally I figured the way to do it using OutputFormat object.
I kinda overlooked the documentation, thanks for pointing me to it.
Thanks!
|
 |
steeve jones
Greenhorn
Joined: Sep 24, 2010
Posts: 3
|
|
|
XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat().setOmitEncoding(true));
|
 |
 |
|
|
subject: Removing encoding attribute from the <XML> element
|
|
|