Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to produce XML DOM indents?  (Read 3805 times)
0 Members and 1 Guest are viewing this topic.
Offline Bombadil

Senior Member





« Posted 2004-05-25 11:47:53 »

Hi,

I'd like to write a formatted (in particular: indented) Xml file, but the following DOM transformer (identity) doesn't indent the elements...
(OK, it's no gaming question but since I use it inside a small game, I am here).

What I would like to have is this (hopefully this board doesn't eat my indents) :
1  
2  
3  
4  
<Sprite Name="SpritenameA">
  <Anim Fps="60"/>
  <Datei Rgb="Rotier.png" Alpha="Rotier_A.png" />
</Sprite>

However what I actually get is: each line starts at the left, ie no spaces. It reads worse when I have nested elements...

Wham am I doing wrong?

J2SE 1.42, ignoring throws for the following example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
TransformerFactory transfabrik = TransformerFactory.newInstance();
Transformer sTransformer = transfabrik.newTransformer();
sTransformer.setOutputProperty(OutputKeys.INDENT, "yes");

Document sDokument = sBuilder.newDocument();
Element dokuElement = (Element) sDokument.createElement("root");
sDokument.appendChild(dokuElement);

Element spriteElm = sDokument.createElement("Sprite");
spriteElm.setAttribute("Name", "Meinspritename-" + i);

Element animElm = sDokument.createElement("Anim");
animElm.setAttribute("Fps", "60");
spriteElm.appendChild(animElm);
...
dokuElement.appendChild(spriteElm);
dokuElement.appendChild(sDokument.createTextNode("\n\n"));

DOMSource    domquelle = new DOMSource(sDokument);
StreamResult ausgabe   = new StreamResult(System.out); // new File("Datei.xml"));
sTransformer.transform(domquelle, ausgabe);
}
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #1 - Posted 2004-05-25 11:50:59 »

add this:
1  
2  
sTransformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount","4"); // new property
sTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); // old property

Offline Bombadil

Senior Member





« Reply #2 - Posted 2004-05-25 11:54:45 »

Quote
add this:
(..)

Well, I love such short and efficient problem fixings. :-)
Thanks a lot, Swpalmer.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bombadil

Senior Member





« Reply #3 - Posted 2004-10-09 07:18:01 »

With Java 1.5 it doesn't indent anymore. :-(
Couldn't find anything in the release notes about changes. However a google reports: "Indentation is broken in jdk1.5 beta"

Does this mean it's also broken in Jdk 1.5 final?
The bugdatabase at
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5064280
suggests some "workaround" and doesn't consider it as bug.

How to use these workarounds, please?
Offline Bombadil

Senior Member





« Reply #4 - Posted 2004-10-12 04:32:53 »

Knock knock on heaven's door...
:-)

(The Korean version of course, as found in the movie "Windstruck")
Offline Mark Thornton

Senior Member





« Reply #5 - Posted 2004-10-12 13:48:21 »

Quote
With Java 1.5 it doesn't indent anymore. :-(
Couldn't find anything in the release notes about changes. However a google reports: "Indentation is broken in jdk1.5 beta"

Does this mean it's also broken in Jdk 1.5 final?
The bugdatabase at
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5064280
suggests some "workaround" and doesn't consider it as bug.

How to use these workarounds, please?

Just from reading the bug report it would appear that it was the namespace of the feature which changed --- the option was still there. j2se5 final should support the old name as well as the new, otherwise replace http://xml.apache.org/xslt in your code with http://xml.apache.org/xalan
Offline Bombadil

Senior Member





« Reply #6 - Posted 2004-10-12 14:31:51 »

Quote

Just from reading the bug report it would appear that it was the namespace of the feature which changed --- the option was still there. j2se5 final should support the old name as well as the new, otherwise replace http://xml.apache.org/xslt in your code with http://xml.apache.org/xalan

I tried both variants but with no luck. With Java 1.42 the same code outputs indents like expected, but not on java 1.50... :-(

This is how I feed the Xml document:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
private static Document        sDokument;
private static DocumentBuilder sBuilder;
private static Transformer     sTransformer;

// Konstruktor
DocumentBuilderFactory fabrik = DocumentBuilderFactory.newInstance();
sBuilder = fabrik.newDocumentBuilder();
TransformerFactory transfabrik = TransformerFactory.newInstance();
sTransformer = transfabrik.newTransformer();
sTtransformer.setOutputProperty(OutputKeys.INDENT, "yes");
sTransformer.setOutputProperty("{http://xml.apache.org/xalan} indent-amount", "2");

// Write method is same as in my first article...

Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #7 - Posted 2004-10-13 03:44:28 »

Quote

I tried both variants but with no luck. With Java 1.42 the same code outputs indents like expected, but not on java 1.50... :-(

This is how I feed the Xml document:
1  
2  
3  
4  
5  
...
sTtransformer.setOutputProperty(OutputKeys.INDENT, "yes");
sTransformer.setOutputProperty("{http://xml.apache.org/xalan} indent-amount", "2");

// Write method is same as in my first article...



In the code above you appear to have a space
"{http://xml.apache.org/xalan} indent-amount"
instead of:
"{http://xml.apache.org/xalan}indent-amount"

I'm not sure if that matters at all.

And you can leave both 'xalan' and 'xslt' properties in, one may be ignored by some processors, but the one that matters will be picked up.

Offline Bombadil

Senior Member





« Reply #8 - Posted 2004-10-13 04:51:30 »

Quote

In the code above you appear to have a space
"{http://xml.apache.org/xalan} indent-amount"
instead of:
"{http://xml.apache.org/xalan}indent-amount"

I'm not sure if that matters at all.

Unfortunately it didn't matter (with Java 1.5): no indent.

Quote
And you can leave both 'xalan' and 'xslt' properties in, one may be ignored by some processors, but the one that matters will be picked up.

Ok, I'll do that, thanks for the infos.

However with Java 1.5 none of these work so far.

{Edit}
I'm sure I'm not the first one who noticed this. I've just posted on a XML dedicated forum, too. It's a thread inside the Java Technology Forum, subforum Xml.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #9 - Posted 2004-10-13 11:02:55 »

Well I grepped the source and found that

com/sun/org/apache/xalan/internal/xsltc/compiler/Output.java

seems to have the code on lines 229-248

It is using BOTH of the above properties ('xalan' given priority over 'xslt')

Oddly I can't find any reference to READ the "indent_amount" property that that code sets.  But it does seem to affect the _indentamount integer variablein the Translet class... which is used in a call to handler.setIndentAmount(int)...   Maybe if you set some breakpoints in the Xalan code you can learn more.

What is the output method & media type you are using for the transformer?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bombadil

Senior Member





« Reply #10 - Posted 2004-10-13 11:45:10 »

Quote
It is using BOTH of the above properties ('xalan' given priority over 'xslt')

Ok. Now we can say they both should work.

Quote
Oddly I can't find any reference to READ the "indent_amount" property that that code sets.  But it does seem to affect the _indentamount integer variablein the Translet class... which is used in a call to handler.setIndentAmount(int)...

Oh. It's "indent-amount" actually, not "indent_amount". But I guess you meant that.

Quote
Maybe if you set some breakpoints in the Xalan code you can learn more.
I'm going to do that. Although I'm an XML greenhorn.

Quote
What is the output method & media type you are using for the transformer?

I don't set anything extra with the set...Property lines. Would I have to?


PS: Here's the sumation basic code I use so far
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
// Set up
TransformerFactory transfabrik  = TransformerFactory.newInstance();
Transformer        sTransformer = transfabrik.newTransformer();

sTtransformer.setOutputProperty(OutputKeys.INDENT, "yes");
sTransformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");

// Fill Xml elements
DocumentBuilderFactory fabrik = DocumentBuilderFactory.newInstance();
DocumentBuilder sBuilder = fabrik.newDocumentBuilder();

Document sDokument = sBuilder.newDocument();
Element dokuElement = (Element) sDokument.createElement("root");
sDokument.appendChild(dokuElement);

Element someElement = sDokument.createElement("Mainelement");
someElement.setAttribute("Name", "Myname");

Element anotherElmement = sDokument.createElement("Subelement");
anotherElmement.setAttribute("Bla", "60");
someElement.appendChild(anotherElmement);
// etc
dokuElement.appendChild(someElement);

// Write file
DOMSource domsource = new DOMSource(sDokument);
StreamResult output = new StreamResult(System.out); // new File("File.xml"));
sTransformer.transform(domsource, output);
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #11 - Posted 2004-10-14 21:10:07 »

Quote
Oh. It's "indent-amount" actually, not "indent_amount". But I guess you meant that.

no I meant what I wrote - I'm referring to a java Property in the Xalan source code, not the XSLT transformer property set externally.
Quote


I don't set anything extra with the set...Property lines. Would I have to?

Maybe.  I have:
1  
2  
3  
4  
5  
6  
7  
8  
9  
Transformer trans = TransformerFactory.newInstance().newTransformer();

trans.setOutputProperty(OutputKeys.METHOD, "xml");

trans.setOutputProperty(OutputKeys.VERSION,"1.0");

trans.setOutputProperty(OutputKeys.MEDIA_TYPE,"text/xml");

trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"no");

as well as what you have...  but I just checked and it also fails to indent on Java 5. Lame.

Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #12 - Posted 2004-10-14 21:20:44 »

Could it be THIS bug?

Offline Bombadil

Senior Member





« Reply #13 - Posted 2004-10-17 07:34:14 »

Unfortunately I don't know.

On the other Java forum somebody responded and refered to http://java.sun.com/j2se/1.5.0/docs/guide/xml/jaxp/JAXP-Compatibility_150.html

.. saying this  "intend" feature has never been a a documented feature, so maby it could be OK when it broke now.

If it's never been an official feature: how to intend XML outputs the official way?
Offline Bombadil

Senior Member





« Reply #14 - Posted 2004-10-21 06:32:54 »

Another guy responded in the mentioned Sun forum: http://forum.java.sun.com/thread.jsp?forum=34&thread=562510&message=2783513#2783513

He says this would be the solution for Java 1.5+
Quote

TransformFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", new Integer(4));

... but it doesn't work for me, yet.
Does it for you? In case it does: what's your code looking like, please?
Offline Bombadil

Senior Member





« Reply #15 - Posted 2004-11-20 10:10:17 »

A contributor to the mentioned Java forum thread solved the issue for Java 1.5
Please see article by Philiphooker: http://forum.java.sun.com/thread.jspa?threadID=562510
Offline busuzima

Innocent Bystander





« Reply #16 - Posted 2012-09-25 12:21:36 »

sTransformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount","4"); helped, but it s been applied only for the first line inside the xml doc. How to indent other lines? Thank you, guys!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (2 views)
2014-09-21 02:42:18

BurntPizza (8 views)
2014-09-21 01:30:30

BurntPizza (11 views)
2014-09-21 00:34:41

moogie (10 views)
2014-09-21 00:26:15

UprightPath (23 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!