Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing encoding attribute from the <XML> element

 
Srikanth Madasu
Ranch Hand
Posts: 48
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a class from org.dom4j.io.* package...

documentation here
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat().setOmitEncoding(true));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic