• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing XML declaration in XML file

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
when we create XML element using JDOM, the following line appears by default



which is pretty obvious, but i need to create a XML document where the above line should not be present. simply i need to generate a XML fragment rather than XML document with the above declaration.

is there any way i can get rid of the XML decalration line.

Thanks
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This mandatory beginning for XML document.

i need to generate a XML fragment rather than XML document


did you tried with Element class?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Parameswaran Thangavel:


i need to create a XML document where the above line should not be present.


In that case the document isn't an xml.A document without the prolog is not a xml.
I do not think uisng the xml api's you can do anything like that.you can create the xml document and then serialize to a temporary file , then strip the first line out that.
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, both of you are wrong. It is possible for an XML document to not have a prolog, as long as its encoding is either UTF-8 or UTF-16. However if the question is "How do I make that happen using JDOM?" then perhaps it should be in the forum that deals with JDOM and not this one.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you know the version of xml without a prolog ?
 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The XML declaration is optional for XML documents. If you have one, it must have a version. Both the encoding and standalone values are optional.

The parser can use metadata provided by the server to determine the encoding. If the encoding is not given and no metadata is provided, it is assumed to be UTF-8. Some parsers use the first bytes of the file to guess the encoding.

If no XML declaration is found in the prolog, then version is assumed to be 1.0. If you are using version 1.1, clearly this would mandate including an XML declaration.

The W3C strongly recommends that the XML declaration be included in XML documents.
 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, regarding the original question, I knew that the xslt specs provide a way to suppress the xml declaration, so I looked for it in the JDOM documentation and found this:

getOmitDeclaration

public boolean getOmitDeclaration()

Returns whether the XML declaration will be omitted.

Returns:
whether the XML declaration will be omitted



URL: "http://www.jdom.org/docs/apidocs/org/jdom/output/Format.html#getOmitDeclaration()"

Hope that is what you were looking for...

--K
 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant to post the "SET" version, not the "GET" version. Here it is:

setOmitDeclaration

public Format setOmitDeclaration(boolean omitDeclaration)

This will set whether the XML declaration (<?xml version="1.0"?gt;) will be omitted or not. It is common to omit this in uses such as SOAP and XML-RPC calls.

Parameters:
omitDeclaration - boolean indicating whether or not the XML declaration should be omitted.
Returns:
a pointer to this Format for chaining



URL: "http://www.jdom.org/docs/apidocs/org/jdom/output/Format.html#setOmitEncoding(boolean)"


This ought to do it...

--K
 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok -- last post on this topic! The URL is actually:

"http://www.jdom.org/docs/apidocs/org/jdom/output/Format.html#setOmitDeclaration(boolean)"

The previous urls are right next to this one and would work, but this is more accurate.

--K
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good to know about this.
Does JAXP has something to suppress the initial prolog as JDOM is not jaxp compliant?
[ December 20, 2006: Message edited by: Rahul Bhattacharjee ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic