开发者

missing lines because <br> in xml

开发者 https://www.devze.com 2023-01-15 15:13 出处:网络
i have a xml like this: <cont><?php echo nl2br($cont); ?></cont> the response is: <cont>

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 &开发者_JS百科lt;br>)

how can i add the <br>?

thank you!


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>


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

0

精彩评论

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