Bombadil
|
 |
«
Posted
2004-05-25 13: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); sTransformer.transform(domquelle, ausgabe); } |
|
|
|
|
|
swpalmer
|
 |
«
Reply #1 - Posted
2004-05-25 13:50:59 » |
|
add this: 1 2
| sTransformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount","4"); sTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); |
|
|
|
|
Bombadil
|
 |
«
Reply #2 - Posted
2004-05-25 13:54:45 » |
|
add this: (..) Well, I love such short and efficient problem fixings. :-) Thanks a lot, Swpalmer.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Bombadil
|
 |
«
Reply #3 - Posted
2004-10-09 09: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=5064280suggests some "workaround" and doesn't consider it as bug. How to use these workarounds, please?
|
|
|
|
|
Bombadil
|
 |
«
Reply #4 - Posted
2004-10-12 06:32:53 » |
|
Knock knock on heaven's door... :-)
(The Korean version of course, as found in the movie "Windstruck")
|
|
|
|
|
Mark Thornton
|
 |
«
Reply #5 - Posted
2004-10-12 15:48:21 » |
|
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=5064280suggests 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
|
|
|
|
|
Bombadil
|
 |
«
Reply #6 - Posted
2004-10-12 16:31:51 » |
|
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/xalanI 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;
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");
|
|
|
|
|
|
swpalmer
|
 |
«
Reply #7 - Posted
2004-10-13 05:44:28 » |
|
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");
|
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.
|
|
|
|
Bombadil
|
 |
«
Reply #8 - Posted
2004-10-13 06:51:30 » |
|
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. 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.
|
|
|
|
|
swpalmer
|
 |
«
Reply #9 - Posted
2004-10-13 13: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!
|
|
Bombadil
|
 |
«
Reply #10 - Posted
2004-10-13 13:45:10 » |
|
It is using BOTH of the above properties ('xalan' given priority over 'xslt') Ok. Now we can say they both should work. 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. 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. 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
| TransformerFactory transfabrik = TransformerFactory.newInstance(); Transformer sTransformer = transfabrik.newTransformer();
sTtransformer.setOutputProperty(OutputKeys.INDENT, "yes"); sTransformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
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); dokuElement.appendChild(someElement);
DOMSource domsource = new DOMSource(sDokument); StreamResult output = new StreamResult(System.out); sTransformer.transform(domsource, output); |
|
|
|
|
|
swpalmer
|
 |
«
Reply #11 - Posted
2004-10-14 23:10:07 » |
|
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.
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.
|
|
|
|
swpalmer
|
 |
«
Reply #12 - Posted
2004-10-14 23:20:44 » |
|
|
|
|
|
|
|
Bombadil
|
 |
«
Reply #14 - Posted
2004-10-21 08:32:54 » |
|
Another guy responded in the mentioned Sun forum: http://forum.java.sun.com/thread.jsp?forum=34&thread=562510&message=2783513#2783513He says this would be the solution for Java 1.5+ 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?
|
|
|
|
|
|
|
busuzima
|
 |
«
Reply #16 - Posted
2012-09-25 14: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!
|
|
|
|
|
|