foreach 循环创建一个 xml 字符串

我需要从带有数据的数组创建一个 xml 提要,所以我使用 foreach 循环来创建它,但是当我 print_r 结果时,唯一打印出来的部分是 foreach 循环开始之前的部分,我在外面测试了数组它有数据并且正确显示


<PriceHeader>

                          <version>1.5.1</version>

                        </PriceHeader>';

                        foreach($sorted_data as $data){ 

                        '<Price>

                            <itemIdentifier>

                              <sku>'.$data["SKU"].'</sku>

                            </itemIdentifier>

                            <pricingList>

                                <pricing>

                                    <currentPrice>

                                        <value currency="USD" amount='.$data["Price"].'></value>

                                    </currentPrice>

                                </pricing>

                            </pricingList>

                        </Price>';

                        } 

                        '</PriceFeed>';

如果我 print_r 那个 var,我得到的唯一输出是 1.5.1,但是检查 chrome 中的元素显示它也创建了 html 结构,直到循环开始的地方......


LEATH
浏览 177回答 2
2回答

qq_遁去的一_1

您可以SimpleXMLElement在 PHP 中使用。欲了解更多信息,您可以访问PHP.net此外,如果您想在浏览器中查看您的 XML,您可以使用htmlspecialchars带有 echo 的函数。我试图更改您的代码作为示例。我希望它对你有帮助。有一个很好的编码:)<?php$string = "";$string .="<PriceFeed><PriceHeader>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.5.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </PriceHeader>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($sorted_data as $data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $string.= "<Price>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <itemIdentifier>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <sku>".$data["SKU"]."</sku>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </itemIdentifier>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <pricingList>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <pricing>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <currentPrice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value currency=\"USD\" amount=\"".$data["Price"]."\"></value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </currentPrice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </pricing>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </pricingList>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Price>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $string.= "</PriceFeed>";$xml = new SimpleXMLElement($string);$output = $xml->asXML();echo "<pre>";echo htmlspecialchars($output);echo "</pre>";?>

潇湘沐

我通过使用 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){&nbsp; &nbsp; xmlwriter_start_element($xw, 'Price');&nbsp; &nbsp; xmlwriter_start_element($xw, 'itemIdentifier');&nbsp; &nbsp; xmlwriter_start_element($xw, 'sku');&nbsp; &nbsp; xmlwriter_text($xw, $data["SKU"]);&nbsp; &nbsp; xmlwriter_end_element($xw);&nbsp; &nbsp; xmlwriter_end_element($xw);&nbsp; &nbsp; xmlwriter_start_element($xw, 'pricingList');&nbsp; &nbsp; xmlwriter_start_element($xw, 'pricing');&nbsp; &nbsp; xmlwriter_start_element($xw, 'currentPrice');&nbsp; &nbsp; xmlwriter_start_element($xw, 'value');&nbsp; &nbsp; xmlwriter_start_attribute($xw, 'currency');&nbsp; &nbsp; xmlwriter_text($xw, 'USD');&nbsp; &nbsp; xmlwriter_start_attribute($xw, 'amount');&nbsp; &nbsp; xmlwriter_text($xw, $data['Price']);&nbsp; &nbsp; xmlwriter_end_attribute($xw);&nbsp; &nbsp; xmlwriter_end_element($xw);&nbsp; &nbsp; xmlwriter_end_element($xw);&nbsp; &nbsp; xmlwriter_end_element($xw);&nbsp; &nbsp; xmlwriter_end_element($xw);&nbsp; &nbsp; xmlwriter_end_element($xw);&nbsp; &nbsp; xmlwriter_end_element($xw);}xmlwriter_end_document($xw);echo xmlwriter_output_memory($xw);
打开App,查看更多内容
随时随地看视频慕课网APP