java-xmlbuilder icon indicating copy to clipboard operation
java-xmlbuilder copied to clipboard

Don't save correct xml

Open ogbozoyan opened this issue 2 years ago • 2 comments

Hi, java-xmlbuilder version used

<dependency>
			<groupId>com.jamesmurty.utils</groupId>
			<artifactId>java-xmlbuilder</artifactId>
			<version>1.3</version>
		</dependency>

While debugging i checked what contain my XMLBuilder2 object and throughout the process xml tree was correct, but when i'm saving in file

Properties outputProperties = new Properties();
outputProperties.put(javax.xml.transform.OutputKeys.METHOD, "xml");
outputProperties.put(javax.xml.transform.OutputKeys.INDENT, "yes");
outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "2");
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
 PrintWriter writer = new PrintWriter(new FileOutputStream(entity.getId()+"test.xml"));
xmlBuilder2.toWriter(writer,outputProperties);

i got only

XMLBuilder2 xmlBuilder = XMLBuilder2.create("feed")
                .e("feed_version").t("2")
                .up();//feed_version

<feed> <feed_version>2</feed_version> </feed>

all other concatenated tags with attributes just being ignored

ogbozoyan avatar Feb 19 '23 11:02 ogbozoyan

Hi, those output settings generally work but not always, it depends on the underlying XML library understanding and respecting those settings.

If you are using an XML library that doesn't respect these settings, you will need to find alternatives that do work. The XMLBuilder2 helper just delegates to a Transformer to do the serialisation and passes the output options to it, see https://github.com/jmurty/java-xmlbuilder/blob/master/src/main/java/com/jamesmurty/utils/BaseXMLBuilder.java#L1288-L1329 You might need to experiment by making your own Transformer and trying different options.

I also did a quick Google search to see if there's a newer or better way to pretty-print XML documents in Java, since using these arcane properties always seemed like an awful hack. But so many of the results are still from 10–15 years ago, so it seems things haven't improved in all that time.

jmurty avatar Feb 22 '23 11:02 jmurty

i found out issue, i was using wrong variable :D but i found another issue, when i trying generate dynamicly after separator ';' i getting closing tag

ogbozoyan avatar Feb 22 '23 11:02 ogbozoyan