2 回答
TA贡献1725条经验 获得超7个赞
您可以SimpleXMLElement在 PHP 中使用。欲了解更多信息,您可以访问PHP.net
此外,如果您想在浏览器中查看您的 XML,您可以使用htmlspecialchars带有 echo 的函数。
我试图更改您的代码作为示例。
我希望它对你有帮助。
有一个很好的编码:)
<?php
$string = "";
$string .="<PriceFeed>
<PriceHeader>
<version>1.5.1</version>
</PriceHeader>";
foreach($sorted_data as $data){
$string.= "<Price>
<itemIdentifier>
<sku>".$data["SKU"]."</sku>
</itemIdentifier>
<pricingList>
<pricing>
<currentPrice>
<value currency=\"USD\" amount=\"".$data["Price"]."\"></value>
</currentPrice>
</pricing>
</pricingList>
</Price>";
}
$string.= "</PriceFeed>";
$xml = new SimpleXMLElement($string);
$output = $xml->asXML();
echo "<pre>";
echo htmlspecialchars($output);
echo "</pre>";
?>
TA贡献1816条经验 获得超6个赞
我通过使用 XMLwriter 创建 xml 字符串来修复它,以下是代码的结束方式:
$xw = xmlwriter_open_memory();
xmlwriter_set_indent($xw, 1);
$res = xmlwriter_set_indent_string($xw, ' ');
xmlwriter_start_document($w, '1.0', 'UTF-8');
xmlwriter_start_element($xw, 'PriceFeed');
xmlwriter_start_attribute($xw, 'xmlns');
xmlwriter_text($xw, 'http://walmart.com/');
xmlwriter_end_attribute($xw);
xmlwriter_start_element($xw, 'PriceHeader');
xmlwriter_start_element($xw, 'version');
xmlwriter_text($xw, '1.5.1');
xmlwriter_end_element($xw);
xmlwriter_end_element($xw);
foreach($sorted_data as $data){
xmlwriter_start_element($xw, 'Price');
xmlwriter_start_element($xw, 'itemIdentifier');
xmlwriter_start_element($xw, 'sku');
xmlwriter_text($xw, $data["SKU"]);
xmlwriter_end_element($xw);
xmlwriter_end_element($xw);
xmlwriter_start_element($xw, 'pricingList');
xmlwriter_start_element($xw, 'pricing');
xmlwriter_start_element($xw, 'currentPrice');
xmlwriter_start_element($xw, 'value');
xmlwriter_start_attribute($xw, 'currency');
xmlwriter_text($xw, 'USD');
xmlwriter_start_attribute($xw, 'amount');
xmlwriter_text($xw, $data['Price']);
xmlwriter_end_attribute($xw);
xmlwriter_end_element($xw);
xmlwriter_end_element($xw);
xmlwriter_end_element($xw);
xmlwriter_end_element($xw);
xmlwriter_end_element($xw);
xmlwriter_end_element($xw);
}
xmlwriter_end_document($xw);
echo xmlwriter_output_memory($xw);
- 2 回答
- 0 关注
- 174 浏览
添加回答
举报