Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » How do I format XML programmatically?
How do I format XML programmatically? [message #199016] Mon, 12 March 2007 13:54 Go to next message
Eclipse UserFriend
Originally posted by: subramanianj.schneider.com

I am creating XML using DOM and would like to format programmatically for
the user. Could you please help me format XML file programmatically.

thanks,JP
Re: How do I format XML programmatically? [message #199104 is a reply to message #199016] Mon, 12 March 2007 17:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------070908000401030701090102
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

JP,

The APIs for doing this are pretty sucky and change from JVM to JVM and
version to version. This is how we create a transformer that does
indenting that works on all the JVMs:

public Transformer createTransformer(String encoding) throws
TransformerException
{
TransformerFactory transformerFactory =
TransformerFactory.newInstance();

try
{
transformerFactory.setAttribute("indent-number", new Integer(2));
}
catch (IllegalArgumentException exception)
{
// Ignore
}

Transformer transformer = transformerFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");

// Unless a width is set, there will be only line breaks but no
indentation.
// The IBM JDK and the Sun JDK don't agree on the property name,
// so we set them both.
//

transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount",
"2");

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
"2");
if (encoding != null)
{
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
}
return transformer;
}





JP Subramanian wrote:
> I am creating XML using DOM and would like to format programmatically
> for the user. Could you please help me format XML file programmatically.
>
> thanks,JP
>


--------------070908000401030701090102
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
JP,<br>
<br>
The APIs for doing this are pretty sucky and change from JVM to JVM and
version to version.
Re: How do I format XML programmatically? [message #199124 is a reply to message #199104] Mon, 12 March 2007 19:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: subramanianj.schneider.com

Thanks, Ed. It worked. You are the man.
Re: How do I format XML programmatically? [message #199179 is a reply to message #199104] Tue, 13 March 2007 08:34 Go to previous messageGo to next message
Jörg Thönnes is currently offline Jörg ThönnesFriend
Messages: 229
Registered: July 2009
Senior Member
Hi Ed,

quick question: Would this also work with StAX (JP wrote he is using DOM)?

Cheers, Jörg

On 03/12/2007 06:46 PM, Ed Merks wrote:
> JP,
>
> The APIs for doing this are pretty sucky and change from JVM to JVM and
> version to version. This is how we create a transformer that does
> indenting that works on all the JVMs:
>
> public Transformer createTransformer(String encoding) throws
> TransformerException
> {
> TransformerFactory transformerFactory =
> TransformerFactory.newInstance();
>
> try
> {
> transformerFactory.setAttribute("indent-number", new Integer(2));
> }
> catch (IllegalArgumentException exception)
> {
> // Ignore
> }
>
> Transformer transformer = transformerFactory.newTransformer();
>
> transformer.setOutputProperty(OutputKeys.INDENT, "yes");
> transformer.setOutputProperty(OutputKeys.METHOD, "xml");
>
> // Unless a width is set, there will be only line breaks but no
> indentation.
> // The IBM JDK and the Sun JDK don't agree on the property name,
> // so we set them both.
> //
>
> transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount",
> "2");
>
> transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
> "2");
> if (encoding != null)
> {
> transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
> }
> return transformer;
> }
>
>
>
>
>
> JP Subramanian wrote:
>> I am creating XML using DOM and would like to format programmatically
>> for the user. Could you please help me format XML file programmatically.
>>
>> thanks,JP
>>
>
>
Re: How do I format XML programmatically? [message #199513 is a reply to message #199179] Wed, 14 March 2007 18:01 Go to previous message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Jörg,

Sorry, I know nothing about StAX.


Jörg Thönnes wrote:
> Hi Ed,
>
> quick question: Would this also work with StAX (JP wrote he is using
> DOM)?
>
> Cheers, Jörg
>
> On 03/12/2007 06:46 PM, Ed Merks wrote:
>> JP,
>>
>> The APIs for doing this are pretty sucky and change from JVM to JVM
>> and version to version. This is how we create a transformer that
>> does indenting that works on all the JVMs:
>>
>> public Transformer createTransformer(String encoding) throws
>> TransformerException
>> {
>> TransformerFactory transformerFactory =
>> TransformerFactory.newInstance();
>>
>> try
>> {
>> transformerFactory.setAttribute("indent-number", new
>> Integer(2));
>> }
>> catch (IllegalArgumentException exception)
>> {
>> // Ignore
>> }
>>
>> Transformer transformer = transformerFactory.newTransformer();
>>
>> transformer.setOutputProperty(OutputKeys.INDENT, "yes");
>> transformer.setOutputProperty(OutputKeys.METHOD, "xml");
>>
>> // Unless a width is set, there will be only line breaks but no
>> indentation.
>> // The IBM JDK and the Sun JDK don't agree on the property name,
>> // so we set them both.
>> //
>>
>> transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount",
>>
>> "2");
>>
>> transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
>>
>> "2");
>> if (encoding != null)
>> {
>> transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
>> }
>> return transformer;
>> }
>>
>>
>>
>>
>>
>> JP Subramanian wrote:
>>> I am creating XML using DOM and would like to format
>>> programmatically for the user. Could you please help me format XML
>>> file programmatically.
>>>
>>> thanks,JP
>>>
>>
>>
Previous Topic:Modelling Plugins
Next Topic:Strange behaviour with EMF plugins
Goto Forum:
  


Current Time: Tue Apr 16 20:44:19 GMT 2024

Powered by FUDForum. Page generated in 1.12598 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top