views:

44

answers:

2

Hi!

i have a xml like this:

<cont><?php echo nl2br($cont); ?></cont>

the response is:

<cont>
2<br/>
3<br/>
4<br/>
</cont>

in the page the code is:

mainParentElement = document.getElementById('div_cont');
RemoveAllChildElements(mainParentElement);
mainParentElement.innerHTML = "<div class=\'div_cont\'>" + xmlDoc.getElementsByTagName('cont')[0].childNodes[0].nodeValue +  "</div>";

in this case the result in the div is only the first line. (2) if i deleted the nl2br the result is all the lines. (of course with out the <br>)

how can i add the <br>?

thank you!

A: 

Read about CDATA.

There are characters that are not allowed as content of XML tags, like <, because for the parser they mark the beginning of a tag (obviously).
So if you want to have such data as content of a tag, you have to escape it somehow. In XML this is done by wrapping the content into <![CDATA[ ... ]]>, which explicitly tells the parser to not interpret the data contained (treat it as C haracter DATA).

Basically you have to do:

<cont><![CDATA[<?php echo nl2br($cont); ?>]]></cont>
Felix Kling
thank you! in IE it's working but in FF not. any other solution?
Ronny
it was my problem. thank you
Ronny
A: 

adding <br/> will create new xml nodes. your content is then divided into :

2 (text node)
<br/> (xml node)
3 (text node)
<br/> (xml node)
4 (text node)
<br/> (xml node)

(xml node for the lack of a better word)

why do you need to add those linebreaks? if it's for presentation, i suggest adding them just before presenting them

knittl
it is for presentation. can you pls explain to me how to add the breaks? ( i edited the question with the relevant code)
Ronny