开发者

Expressing markup in Java XML property files: CDATA vs. escaped tags

开发者 https://www.devze.com 2022-12-18 18:12 出处:网络
I am re开发者_Go百科ading and writing Java Properties files in XML format. Many of the property value have HTML embedded, which developers wrap in [[CDATA elements like so:

I am re开发者_Go百科ading and writing Java Properties files in XML format. Many of the property value have HTML embedded, which developers wrap in [[CDATA elements like so:

<entry key="foo"><![CDATA[
    <b>bar</b>
]]></entry>

However, when I use the Java API to load these properties and later write them back to XML, it doesn't wrap these entries in CDATA elements, but rather escapes the tags, like so:

<entry key="foo">&lt;b&gt;bar&lt;/b&gt;</entry>

Are these two formats equivalent? Am I introducing any potential problems by replacing CDATA with escaped tags?


Not equivalent, but the text value you get by calling getText() is the same.

However, I would suggest you to abandon Properties in favor of real XML parsed by JAXB - it's awesome, you'll like it.

Didn't found any nice one, so at least these:

Object -> XML: here

Sun's verbose tutorial: http://java.sun.com/webservices/docs/2.0/tutorial/doc/JAXBUsing.html


When the files are loaded into memory in a Properties object there is no difference between the two formats you have shown, as Ondra Žižka an answer with. CDATA sections are a way to escape a block of text instead of escaping every character in it.

I would consider the non-xml property file format myself, you will continue to see the tags in the raw files, but newline characters would need to be escaped.


Yes you could be inducing some problems, depending on how the data is used.

For example if you use it in a HTML page, A<br>B will print as

A

B

But A&lt;br&gt;B will show as

A<br>B
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号