1

I have a problem. I have 2 files. First file data.php:

<?php
$str = '<?xml version="1.0"?>
<products>
    <product>
        <name>Radio</name>
        <price>79.99</price>
        <desc>Place for describe</desc>
    </product>
        <product>
        <name>TV</name>
        <price>599.99</price>
        <desc>Place for describe</desc>
    </product>
    <product>
        <name>Book</name>
        <price>75.00</price>
        <desc>Place for describe</desc>
    </product>
<products>'; 

?>

And the second file index.php:

<?php
include('data.php');

$products =new SimpleXMLElement($str);

foreach($products->product as $product){
echo $product->name;
echo "<br>";
}

?>

I use Wamserver. When i try to run index.php in web Browser I get errors: http://zapodaj.net/images/fbd51b9ca6da9.jpg I want to show name of each product. Can someone can tell me how to solve this problem?

3 Answers 3

1

Your products end block is wrong, you forgot /

Pleas find correct xml

<?xml version="1.0"?>
<products>
    <product>
        <name>Radio</name>
        <price>79.99</price>
        <desc>Place for describe</desc>
    </product>
    <product>
        <name>TV</name>
        <price>599.99</price>
       <desc>Place for describe</desc>
    </product>
    <product>
        <name>Book</name>
        <price>75.00</price>
        <desc>Place for describe</desc>
    </product>
</products>

You can used this website to check your xml online : http://www.xmlvalidation.com/

Sign up to request clarification or add additional context in comments.

Comments

1

First of all, the XML is not valid, you need a closing tag at the end:

    </product>
</products>'; 

(last lines of the file). The rest of the code seems ok.

Secondly, you should use

simplexml_load_string($str);

Example (with php -a):

$ php -a
Interactive mode enabled

php > $str='<?xml version="1.0"?>
php ' <products>
php '     <product>
php '         <name>Radio</name>
php '         <price>79.99</price>
php '         <desc>Place for describe</desc>
php '     </product>
php '         <product>
php '         <name>TV</name>
php '         <price>599.99</price>
php '         <desc>Place for describe</desc>
php '     </product>
php '     <product>
php '         <name>Book</name>
php '         <price>75.00</price>
php '         <desc>Place for describe</desc>
php '     </product>
php ' </products>';
php > $products = simplexml_load_string($str);
php > foreach($products->product as $product){
php { echo $product->name;
php { echo "<br>";
php { }
Radio<br>TV<br>Book<br>

Comments

0

As CommuSoft Says You Haven't Closed your XML block

</product>
</products>';

And you should use as CommuSoft says simplexml_load_string($str); As you are storing your XML in a variable as a string.

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.