Java generated XML showing up with special characters in .Net/SQL
We have a java desktop application that uses JAXB to generate an XML file which is then read by a .Net application and stored in a SQL server database.
We're finding that carriage returns in Java and not coming over as carriage return/line feeds in .Net/SQL.
Is there a way to tell Java or Jaxb to include both the carriage return an开发者_开发问答d line feed. Is there a way to get .Net to put them in. Would a CDATA block help?
Thanks.
How exactly are you creating the XML file? Does the data pass through a PrintStream at any point? If so, then maybe that's the point at which the system-dependent line separators are introduced.
And where exactly are the problematic newlines? Inside a text element? Or between XML tags? If it's the former, then you should be focusing on the code that builds the text, if the latter, then it's the XML generation library.
You could address this by having JAXB not add the indents:
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); // DEFAULT
If you are using the JAXB class to save to XML it will automatically format the document. To remove formatting don't do the following, simply use the above approach:
JAXB.marshal(object, System.out);
You could also try another JAXB implementation. I lead the MOXy JAXB implementation. When our implementation formats the document the following is used
System.getProperty("line.separator");
We ended up fixing them on the .net side.
We pass each string through a function that does a replace of vblf with vbcrlf. Seems to work so far.
In fact the given response of Blaise Doughan was quite fine (Myabe a typo ?)
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Blaise wrote 'false' here
For me it preserves the Newlines...
精彩评论