0

I need to create a xml feed from an array with data, so im using a foreach loop to create it, but when i print_r the result, the only parts that prints out is the part before the foreach loop starts, i tested the array outside and it has data and its correctly diplayed

<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>';

if i print_r that var, The only output i've got is 1.5.1, but inspecting element in chrome shows me that it creates the html structure as well, up to the point where the loop starts....

3
  • 1
    What are you expecting when you have things like '</PriceFeed>';? I would also recommend creating the content using something like SimpleXML rather than building strings. Commented Sep 12, 2019 at 17:09
  • You cannot simply enclose a string in the loop. You need to enlose a command. So maybe an echo commend outputting that string. Commented Sep 12, 2019 at 17:27
  • can you provide an example? Commented Sep 12, 2019 at 17:43

2 Answers 2

1

You can use SimpleXMLElement in PHP. For more information, you can visit PHP.net

Also, if you want to see your XML in your browser, you can use htmlspecialchars function with echo.

I tried to change your code as example.

I hope it will be helpful for you.

Have a nice coding :)

<?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>";
?>
Sign up to request clarification or add additional context in comments.

3 Comments

perfect, that worked better than mine, cause it was accepted by walmart feed, although i dont understand what $string .= means, is that for concatenation?
thank you. Yes it means concatenation of strings. Like that: $str1 = "bla1" $str2 = "bla2" $str = $str1.$str2 [bla1bla2]
Why are you parsing and serializing the XML after creating the XML as text? That is an unnecessary step and does not change anything.
0

I fixed it by creating the xml string with XMLwriter, here is how the code ended:

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.