Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't save correct xml #25

Open
ogbozoyan opened this issue Feb 19, 2023 · 2 comments
Open

Don't save correct xml #25

ogbozoyan opened this issue Feb 19, 2023 · 2 comments

Comments

@ogbozoyan
Copy link

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

@jmurty
Copy link
Owner

jmurty commented Feb 22, 2023

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.

@ogbozoyan
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants