Advertisement

Newline doesn't get parsed

Started by February 19, 2015 07:49 PM
2 comments, last by kseh 9 years, 10 months ago

Hi,

not really the right forum or website at all I guess but I don't know where to ask else.

I am using XML files to read and write settings in my program.

For this task I am using an XMLStreamLibrary which works fine and perfect.

However, when I am trying to write an attribute containing newline characters the library just writes the newline characters into the xml file like this


<content text="Line1
Line2"></content>

but actually it should look like this


<content text="Line1&#13;&#10;Line2"></content>

when I am manually replacing the "newline characters" in my string with "&#13;&#10;" it is writing this


<content text="Line1&amp;#13;&amp;#10;Line2"></content>

because the library is parsing the "&" into "&amp;" which is kind of counter productive in this case.

(It works the other way around though, when I am reading the file in it replaces "&#13;&#10;" with the appropriate newline characters.)

Any idea how I can overcome this dilemma?

Thanks

Best Regards

*Edit: I am using the XMLStreamWriter in Java, I left this information out because I hoped there is an XML based solution for any kind of library.

Yikes, that sounds like a problem.

Question: are these files only going to be used by your program(s) or are they meant to be used elsewhere too? (EDIT: just realized you said your program's settings, so welp) If the former, what happens if you just let it write newlines as it's doing right now and then try to load them back? Do you get the newlines or do you get spaces? Because maybe in practice it's a non-problem (even if annoying).

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
Advertisement
I don't know that specific library, but many XML libraries have flags that control whitespace processing and normalization. Maybe look into the documentation for how you're generating the text serializer and see if there's a flag you have to pass to enable or disable the newline processing.

https://stackoverflow.com/questions/2004386/how-to-save-newlines-in-xml-attribute indicates that what you're asking for is just impossible in some XML libraries, unfortunately (at least without modifying the underlying source code).

Sean Middleditch – Game Systems Engineer – Join my team!

I'm no XML expert but, is there some reason you need to put the text in an attribute and can't put it in the body (assuming that new lines might be handled there better)? What sort of setting is this for?

This topic is closed to new replies.

Advertisement