File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Removing encoding attribute from the <XML> element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Removing encoding attribute from the <XML> element" Watch "Removing encoding attribute from the <XML> element" New topic
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: 12803
    
    5
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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: 18651
    
    8

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));
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing encoding attribute from the <XML> element