轻微更改后PHP SimpleXML失败

时间:2014-11-24 18:24:00

标签: php xml simplexml

我有一个脚本,它接受表单数据并将其提交到XML文件,但是在添加一些额外的节点之后,脚本返回一个空白页面并且无法写入XML,甚至没有返回echo“add another”at结束。我完全不知道这个。咨询我们的服务器提供商后,一切看起来都很好。有什么建议吗?

继承原始代码:

include ('resources/header.php');    
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('../products/pending/products_pending.xml');

$element = $xml->getElementsByTagName('product')->item(0);

$name = $element->getElementsByTagName('name')->item(0);
$description = $element->getElementsByTagName('description')->item(0);
$price = $element->getElementsByTagName('price')->item(0);
$our_price = $element->getElementsByTagName('ourprice')->item(0);
$quantity = $element->getElementsByTagName('quantity')->item(0);
$size = $element->getElementsByTagName('size')->item(0);
$color = $element->getElementsByTagName('color')->item(0);
$vendor = $element->getElementsByTagName('vendor')->item(0);
$category = $element->getElementsByTagName('category')->item(0);
$subcategory = $element->getElementsByTagName('subcategory')->item(0);

$newItem = $xml->createElement('product');

$newItem->appendChild($xml->createElement('inventory_type', 'inventory_type'));
$newItem->appendChild($xml->createElement('model_number', 'model_number'));
$newItem->appendChild($xml->createElement('sku', $product_sku));
$newItem->appendChild($xml->createElement('category', $_POST['product_category']));
$newItem->appendChild($xml->createElement('subcategory', $_POST['product_subcategory']));
$newItem->appendChild($xml->createElement('description', $_POST['product_description']));
$newItem->appendChild($xml->createElement('manufacturer', 'manufacturer'));
$newItem->appendChild($xml->createElement('barcode_upc', 'barcode_upc'));
$newItem->appendChild($xml->createElement('color', $_POST['product_color']));
$newItem->appendChild($xml->createElement('size', $_POST['product_size']));
$newItem->appendChild($xml->createElement('min', 'min'));
$newItem->appendChild($xml->createElement('max', 'max'));
$newItem->appendChild($xml->createElement('quantity', $_POST['product_quantity']));
$newItem->appendChild($xml->createElement('avg_cost', 'avg_cost'));
$newItem->appendChild($xml->createElement('freight', 'freight'));
$newItem->appendChild($xml->createElement('w_cost', '$'.$update_our_price));
$newItem->appendChild($xml->createElement('retail', '$'.$_POST['product_price']));
$newItem->appendChild($xml->createElement('our_price', '$'.$_POST['product_price']));
$newItem->appendChild($xml->createElement('min_price', '$'.$_POST['product_price']));
$newItem->appendChild($xml->createElement('p_vendor_number', $update_vendor));
$newItem->appendChild($xml->createElement('p_vendor_item_number', $vendor_item_no));
$newItem->appendChild($xml->createElement('name', $_POST['product_name']));
$newItem->appendChild($xml->createElement('price', '$'.$_POST['product_price']));
$newItem->appendChild($xml->createElement('ourprice', '$'.$update_our_price));
$newItem->appendChild($xml->createElement('vendor', $update_vendor));
$newItem->appendChild($xml->createElement('consignmentfee', $consignment_fee));
$newItem->appendChild($xml->createElement('websiteprice', '$'.$update_website_price));
$newItem->appendChild($xml->createElement('image', $image));
$newItem->appendChild($xml->createElement('status', 'Pending'));

$xml->getElementsByTagName('pending_products')->item(0)->appendChild($newItem);

$xml->save('../products/pending/products_pending.xml');

echo '<div class="container content">';
echo '<a href="add_product.php">Add Another</a>';
include ('resources/footer.php');

这是新代码:

include ('resources/header.php');

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('../products/pending/products_pending.xml');

$element = $xml->getElementsByTagName('product')->item(0);

$name = $element->getElementsByTagName('name')->item(0);
$description = $element->getElementsByTagName('description')->item(0);
$price = $element->getElementsByTagName('price')->item(0);
$our_price = $element->getElementsByTagName('ourprice')->item(0);
$quantity = $element->getElementsByTagName('quantity')->item(0);
$size = $element->getElementsByTagName('size')->item(0);
$color = $element->getElementsByTagName('color')->item(0);
$vendor = $element->getElementsByTagName('vendor')->item(0);
$category = $element->getElementsByTagName('category')->item(0);
$subcategory = $element->getElementsByTagName('subcategory')->item(0);

$newItem = $xml->createElement('product');

$newItem->appendChild($xml->createElement('inventory_type', 'a'));
$newItem->appendChild($xml->createElement('model_number', $vendor_item_no));
$newItem->appendChild($xml->createElement('sku', $product_sku));
$newItem->appendChild($xml->createElement('category', '24'));
$newItem->appendChild($xml->createElement('subcategory', $_POST['product_category']));
$newItem->appendChild($xml->createElement('description', $_POST['product_description']));
$newItem->appendChild($xml->createElement('manufacturer', 'manufacturer'));
$newItem->appendChild($xml->createElement('barcode_upc', $product_sku));
$newItem->appendChild($xml->createElement('color', $_POST['product_color']));
$newItem->appendChild($xml->createElement('size', $_POST['product_size']));
$newItem->appendChild($xml->createElement('lease', 'test'));
$newItem->appendChild($xml->createElement('book_depr', 'test'));
$newItem->appendChild($xml->createElement('tax_depr', 'test'));
$newItem->appendChild($xml->createElement('min', 'test'));
$newItem->appendChild($xml->createElement('max', 'test'));
$newItem->appendChild($xml->createElement('quantity', $_POST['product_quantity']));
$newItem->appendChild($xml->createElement('avg_cost', 'avg_cost'));
$newItem->appendChild($xml->createElement('freight', 'freight'));
$newItem->appendChild($xml->createElement('w_cost', '$'.$update_our_price));
$newItem->appendChild($xml->createElement('retail', '$'.$_POST['product_price']));
$newItem->appendChild($xml->createElement('our_price', '$'.$_POST['product_price']));
$newItem->appendChild($xml->createElement('min_price', '$'.$_POST['product_price']));
$newItem->appendChild($xml->createElement('price_a', 'test'));
$newItem->appendChild($xml->createElement('price_b', 'test'));
$newItem->appendChild($xml->createElement('price_c', 'test'));
$newItem->appendChild($xml->createElement('spiff', 'test'));
$newItem->appendChild($xml->createElement('p_vendor_number', $update_vendor));
$newItem->appendChild($xml->createElement('p_vendor_item_number', $vendor_item_no));
$newItem->appendChild($xml->createElement('p_vendor_last_cost', 'test'));
$newItem->appendChild($xml->createElement('p_vendor_last_date', 'test'));
$newItem->appendChild($xml->createElement('s1_vendor_number', 'test'));
$newItem->appendChild($xml->createElement('s1_vendor_item_number', 'test'));
$newItem->appendChild($xml->createElement('s1_vendor_last_cost', 'test'));
$newItem->appendChild($xml->createElement('s1_vendor_last_date', 'test'));
$newItem->appendChild($xml->createElement('s2_vendor_number', 'test'));
$newItem->appendChild($xml->createElement('s2_vendor_item_number', 'test'));
$newItem->appendChild($xml->createElement('s2_vendor_last_cost', 'test'));
$newItem->appendChild($xml->createElement('s2_vendor_last_date', 'test'));
$newItem->appendChild($xml->createElement('notes', 'test'));
$newItem->appendChild($xml->createElement('pos_reminder', 'test'));
$newItem->appendChild($xml->createElement('invoice_notes', 'test'));
$newItem->appendChild($xml->createElement('image', $image));
$newItem->appendChild($xml->createElement('selection_code', 'test'));
$newItem->appendChild($xml->createElement('warranty', 'test'));
$newItem->appendChild($xml->createElement('locator', 'test'));
$newItem->appendChild($xml->createElement('bar_label', 'test'));
$newItem->appendChild($xml->createElement('date_in_house', 'test'));
$newItem->appendChild($xml->createElement('unit', 'test'));
$newItem->appendChild($xml->createElement('weight', 'test'));
$newItem->appendChild($xml->createElement('loyalty_exempt', 'test'));
$newItem->appendChild($xml->createElement('food_stamp', 'test'));
$newItem->appendChild($xml->createElement('healthcare', 'test'));
$newItem->appendChild($xml->createElement('scale', 'test'));
$newItem->appendChild($xml->createElement('status', 'Pending'));
$newItem->appendChild($xml->createElement('name', $name));
$newItem->appendChild($xml->createElement('vendor', $vendor));
$newItem->appendChild($xml->createElement('consignmentfee', $consignmentfee));
$newItem->appendChild($xml->createElement('websiteprice', $websiteprice));

$xml->getElementsByTagName('pending_products')->item(0)->appendChild($newItem);

$xml->save('../products/pending/products_pending.xml');

echo '<div class="container content">';
echo '<a href="add_product.php">Add Another</a>';
include ('resources/footer.php');

1 个答案:

答案 0 :(得分:0)

$element->getElementsByTagName()返回一个DomNode对象,但$xml->createElement()要求两个字符串。

因此,当您尝试使用$name$vendor作为值创建元素时,您要求PHP将DomNode强制转换为字符串,但它无法执行此操作。您可能会在日志文件中发现这样的错误: Catchable fatal error: Object of class DOMElement could not be converted to string on line XX

要获取字符串,您需要使用nodeValue属性:

$name = $element->getElementsByTagName('name')->item(0)->nodeValue;
$description = $element->getElementsByTagName('description')->item(0)->nodeValue;
$price = $element->getElementsByTagName('price')->item(0)->nodeValue;
$our_price = $element->getElementsByTagName('ourprice')->item(0)->nodeValue;
$quantity = $element->getElementsByTagName('quantity')->item(0)->nodeValue;
$size = $element->getElementsByTagName('size')->item(0)->nodeValue;
$color = $element->getElementsByTagName('color')->item(0)->nodeValue;
$vendor = $element->getElementsByTagName('vendor')->item(0)->nodeValue;
$category = $element->getElementsByTagName('category')->item(0)->nodeValue;
$subcategory = $element->getElementsByTagName('subcategory')->item(0)->nodeValue;