Hi Erick,

Groovy's XmlUtil is using javax.xml.transform.Transformer
OutputKeys.INDENT property.

Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

In Java 9, the indentation behavior changed. Please note

https://bugs.openjdk.org/browse/JDK-8087303

and

https://bugs.openjdk.org/browse/JDK-8262285

Cheers, 

Jörg

Am Freitag, dem 31.03.2023 um 22:35 +0000 schrieb Nelson, Erick:
> + normalize
>  
> I see no difference with or without normalize
>  
> Java 17 (and same results from 10,18,19)
> en032339@C02CJMZ8MD6M bin % ./java -version
> java version "17.0.6" 2023-01-17 LTS
> Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
> Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed
> mode, sharing)
>  
>  
> en032339@C02CJMZ8MD6M test1 % cat src/test.groovy
> import groovy.xml.*
>  
> String xml = $/<?xml version="1.0" encoding="UTF-8"?>
> <root><erick banana="yellow" apple="red"/><erick crypto-
> banana="yellow" apple="red"/></root>
> /$
>  
> def doc = new XmlParser().parseText(xml)
>  
> new File('test.xml').withWriter('UTF-8') { it <<
> XmlUtil.serialize(doc).normalize() }
> en032339@C02CJMZ8MD6M test1 % cat test.xml       
> <?xml version="1.0" encoding="UTF-8"?><root>
>     
>   <erick banana="yellow" apple="red"/>
>     
>   <erick crypto-banana="yellow" apple="red"/>
>   
> </root>
> en032339@C02CJMZ8MD6M test1 % hexdump -C test.xml
> 00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml
> version="1|
> 00000010 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 |.0"
> encoding="UT|
> 00000020 46 2d 38 22 3f 3e 3c 72 6f 6f 74 3e 0a 20 20 20 |F-
> 8"?><root>.  |
> 00000030 20 0a 20 20 3c 65 72 69 63 6b 20 62 61 6e 61 6e | .  <erick
> banan|
> 00000040 61 3d 22 79 65 6c 6c 6f 77 22 20 61 70 70 6c 65 |a="yellow"
> apple|
> 00000050 3d 22 72 65 64 22 2f 3e 0a 20 20 20 20 0a 20 20 |="red"/>. 
>   .  |
> 00000060 3c 65 72 69 63 6b 20 63 72 79 70 74 6f 2d 62 61 |<erick
> crypto-ba|
> 00000070 6e 61 6e 61 3d 22 79 65 6c 6c 6f 77 22 20 61
> 70 |nana="yellow" ap|
> 00000080 70 6c 65 3d 22 72 65 64 22 2f 3e 0a 20 20 0a
> 3c |ple="red"/>. .<|
> 00000090 2f 72 6f 6f 74 3e 0a                             |/root>.|
> 00000097
>  
>  
> Output using java 8
> en032339@C02CJMZ8MD6M bin % ./java -version   
> java version "1.8.0_251"
> Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
> Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
>  
>  
> en032339@C02CJMZ8MD6M test1 % cat test.xml       
> <?xml version="1.0" encoding="UTF-8"?><root>
>   <erick banana="yellow" apple="red"/>
>   <erick crypto-banana="yellow" apple="red"/>
> </root>
> en032339@C02CJMZ8MD6M test1 % hexdump -C test.xml
> 00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml
> version="1|
> 00000010 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 |.0"
> encoding="UT|
> 00000020 46 2d 38 22 3f 3e 3c 72 6f 6f 74 3e 0a 20 20 3c |F-
> 8"?><root>. <|
> 00000030 65 72 69 63 6b 20 62 61 6e 61 6e 61 3d 22 79 65 |erick
> banana="ye|
> 00000040 6c 6c 6f 77 22 20 61 70 70 6c 65 3d 22 72 65 64 |llow"
> apple="red|
> 00000050 22 2f 3e 0a 20 20 3c 65 72 69 63 6b 20 63 72 79 |"/>. 
> <erick cry|
> 00000060 70 74 6f 2d 62 61 6e 61 6e 61 3d 22 79 65 6c 6c |pto-
> banana="yell|
> 00000070 6f 77 22 20 61 70 70 6c 65 3d 22 72 65 64 22 2f |ow"
> apple="red"/|
> 00000080 3e 0a 3c 2f 72 6f 6f 74 3e 0a                   |>.</root>.|
> 0000008a
>  
>  
> Above code run from Eclipse. Maybe that matters?
>  
> XmlSlurper…. No see none of the above issues.
>  
>  
> From:Paul King <pa...@asert.com.au>
> Date: Friday, March 31, 2023 at 11:27 AM
> To: users@groovy.apache.org <users@groovy.apache.org>
> Subject: [EXT] Re: Extra linefeeds with groovy.xml.XmlUtil.serialize
> If you add ".normalize()", does that make any difference? I am trying
> to determine is there are CR chars in the output. Perhaps piping to
> od or some octal/hex dump program.
>  
> There are also various parser options/features that might be
> different on different platforms.
>  
> Does XmlSlurper exhibit the same behavior?
>  
> Can you try trimWhitespace and keepIgnorableWhitespace on XmlParser?
>  
> There are also setFeature and setProperty methods on XmlParser. I
> don't think they will help in your case but they have helped for
> other tricky XML scenarios.
>  
> Cheers, Paul.
>  
>  
> On Sat, Apr 1, 2023 at 1:24 AM Nelson, Erick
> <erick.nel...@hdsupply.com> wrote:
> >  
> >  
> > From:Nelson, Erick <erick.nel...@hdsupply.com>
> > Date: Friday, March 31, 2023 at 7:53 AM
> > To: users@groovy.apache.org <users@groovy.apache.org>
> > Subject: Re: Extra linefeeds with groovy.xml.XmlUtil.serialize
> > This appears to be a Java problem, not a Groovy problem,  and
> > possibly an OpenJDK issue.
> > I’ve only been able to test this further on my Mac laptop as I
> > cannot install different versions of Java. Redhat ON LINUX
> > Java 8 works as expected
> > I’ve tried Java 10, 11, 17, 18 and 19 and all appear to exhibit
> > this behavior
> >  
> > From:Nelson, Erick <erick.nel...@hdsupply.com>
> > Date: Tuesday, March 21, 2023 at 10:25 AM
> > To: users@groovy.apache.org <users@groovy.apache.org>
> > Subject: Extra linefeeds with groovy.xml.XmlUtil.serialize
> > I get extra line feeds on my mac when running identicalcode. Does
> > anybody know why this would be?
> >  
> > On Linux… (expected output)
> >  
> > [tauser02@cfmips01ld0s work]$ groovy -v
> > Groovy Version: 4.0.10 JVM: 1.8.0_362 Vendor: Red Hat, Inc. OS:
> > Linux
> > [tauser02@cfmips01ld0s work]$ cat test.groovy
> > String xml = $/<?xml version="1.0" encoding="UTF-8"?><root><elem1
> > attr1="value1" attr2="value2"/><elem2 attr1="value4"
> > attr2="value4"/></root>/$
> > def doc = new groovy.xml.XmlParser().parseText(xml)
> > println groovy.xml.XmlUtil.serialize(doc)
> > [tauser02@cfmips01ld0s work]$ groovy test.groovy
> > <?xml version="1.0" encoding="UTF-8"?><root>
> > <elem1 attr1="value1" attr2="value2"/>
> > <elem2 attr1="value4" attr2="value4"/>
> > </root>
> >  
> >  
> >  
> > On my MAC…(note extra line feeds output)
> >  
> > en032339@C02CJMZ8MD6M ~ % groovy -v
> > Groovy Version: 4.0.10 JVM: 18.0.2.1 Vendor: Eclipse Adoptium OS:
> > Mac OS X
> > en032339@C02CJMZ8MD6M ~ % cat test.groovy 
> > String xml = $/<?xml version="1.0" encoding="UTF-8"?><root><elem1
> > attr1="value1" attr2="value2"/><elem2 attr1="value4"
> > attr2="value4"/></root>/$
> > def doc = new groovy.xml.XmlParser().parseText(xml)
> > println groovy.xml.XmlUtil.serialize(doc)
> > en032339@C02CJMZ8MD6M ~ % groovy test.groovy 
> > <?xml version="1.0" encoding="UTF-8"?><root>
> >     
> >  <elem1 attr1="value1" attr2="value2"/>
> >     
> >  <elem2 attr1="value4" attr2="value4"/>
> >   
> > </root>
> >  

Reply via email to