0
我使用下面的代碼在PHP中將值寫入xml文件。在寫入DOM節點時,大字符串會被截斷php
if(isset($_POST['title']) && isset($_POST['content'])){
$news = new DOMDocument();
$news->load('../files/news.xml');
$parentNode = $news->getElementById('newsContainer');
$newsNodes = $news->getElementsByTagName('News');
echo $_POST['content'];
if($newsNodes->length > 0){
$newsItem = $news->createElement('News');
$newsItemTitle = $news->createElement('Title');
$newsItemTitle->nodeValue = $_POST['title'];
$newsItemContent = $news->createElement('Content');
$newsItemContent->nodeValue = $_POST['content']; //The value I assign here gets cut out
$newsItem->appendChild($newsItemTitle);
$newsItem->appendChild($newsItemContent);
$news->documentElement->appendChild($newsItem);
$result = $news->save('../files/news.xml');
if($result == FALSE){
echo 'false';
}else{
echo 'true';
}
}else{
$newNewsContentNode = new DOMDocument();
$newsContainer = $newNewsContentNode->createElement('NewsContainer');
$newsItem = $newNewsContentNode->createElement('News');
$newsItemTitle = $newNewsContentNode->createElement('Title');
$newsItemTitle->nodeValue = $_POST['title'];
$newsItemContent = $newNewsContentNode->createElement('Content');
$newsItemContent->nodeValue = $_POST['content']; //The value I assign here gets cut out
$newsItem->appendChild($newsItemTitle);
$newsItem->appendChild($newsItemContent);
$newsContainer->appendChild($newsItem);
$newNewsContentNode->appendChild($newsContainer);
$result = $newNewsContentNode->save('../files/news.xml');
if($result == FALSE){
echo 'false';
}else{
echo 'true';
}
}
}
我保存在它1065個字符,但是當我檢查XML文件的節點只包含給定的字符串的一部分。我查看了通過POST的字符串,它很好。我在這裏做錯了什麼?
請降低你的示例代碼來演示這個問題的地步。這也應該可以防止你發佈無關的代碼。它應該有助於快速清理事情。 /並且只是一個註釋fyi:PHP中的createElement允許您直接傳遞元素值:https://secure.php.net/manual/en/domdocument.createelement.php – hakre
如果您看到節點值中的字符串已刪除或已刪除,這可能是一個字符編碼問題。這通常只在輸出時纔可見。你必須提供有效的UTF-8字符串,只是分配'$ _POST'字段顯示,你沒有檢查。相關:[正則表達式檢測無效的UTF-8字符串](http://stackoverflow.com/q/11709410/367456) – hakre