meg*_*382 41 php xml simplexml php-7
这是我的代码:
<?php
// 27/01/2016 Edit:
$result = mysql_query("A Long mysql query");
$rss = new SimpleXMLElement('<rss version="2.0" />');
$products = $rss->addChild('products');
///
while($row = mysql_fetch_array($result)){
$product = $products->addChild('category');
$product->addChild('product_id',"$row[product_id]");
$product->addChild('cat_id',"$row[cat_id]");
$product->addChild('cat_name',"$row[cat_name]");
$product->addchild('product_code',"$row[product_code]");
$product->addchild('product_name',"$row[product_name]");
$product->addChild('description','$row[description]');
$product->addchild('rating',"$row[rating]");
$product->addchild('image_url','$row[imag_url]');
$product->addchild('price',"$row[price]");
$product->addchild('discount',"$row[discount]");
$product->addchild('stock_status',"$row[stock_status]");
$product->addchild('stock_quantity',"$row[stock_quantity]");
$product->addchild('weight',"$row[weight]");
$product->addchild('length',"$row[length]");
$product->addchild('width',"$row[width]");
$product->addchild('height',"$row[height]");
$product->addchild('colour',"$row[colour]");
$product->addchild('size',"$row[size]");
$product->addchild('material',"$row[material]");
$product->addchild('pattern',"$row[pattern]");
};
Header('Content-type: text/xml');
print($rss->asXML());
?>
Run Code Online (Sandbox Code Playgroud)
这是错误:
警告:SimpleXMLElement :: addChild()[simplexmlelement.addchild]:第40行的C:\ wamp\www\rabwah\core.php中未终止的实体引用_Coke.jpg
错误符合'$row[imag_url]'.
Joe*_*vey 103
这正确编码& < >和"" ''
$parent->addChild($name, htmlspecialchars($value));
Run Code Online (Sandbox Code Playgroud)
meg*_*382 39
SimpleXMLElement实际上是一个行为像对象的系统资源.这使得使用循环变得棘手.因此,当尝试添加新的子元素而不是:
$product->addchild('element', $value);
Run Code Online (Sandbox Code Playgroud)
做这个:
$product->element = $value;
Run Code Online (Sandbox Code Playgroud)
或者你可以使用htmlspecialchars(),来逃避HTML字符.
注意:
mysql_*从php-5.5开始被弃用,并从php-7开始删除.所以改为使用mysqli_*或PDO.
为什么我不应该在PHP中使用mysql_*函数?
我对此的解决方案是专门创建一个文本节点,以确保绝对正确地避开了所有内容:
$cell = $dom->createElement('td');
$cell->appendChild($dom->createTextNode($value));
Run Code Online (Sandbox Code Playgroud)