Petrus Alli
Petrus Alli

Reputation: 15

Cant get New SimpleXMLElement to work with file

I'm using this code:

  $str_xml = file_get_contents('pricetest1.xml');
  $library = New SimpleXMLElement('srt_xml');
  print_r $library;

The $str_xml is supposed to load from a URL later on, the XML placed is just an example.

The XML looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<products>
  <product id="809809">
    <name>LongJohn</name>
    <brand>7</brand>
    <product_url>https://www.example.com/producturl.html</product_url>
    <image_url>https://www.example.com/product.jpg</image_url>
    <price>369.00</price>
    <former_price>369.00</former_price>
    <in>Y</in>
    <sum>110297</sum>
  </product>
</products>
</xml>

I don't really know how to get any errors out of this since it's just making the page not loading in my browser.

I'm thinking it could be that the XML is in ISO and my page is in UTF, but I'm not sure if this really matters, correct me if I'm wrong.

Also since each product got an "id" inside, is this making some kind of exception needed to be handled?

In the end I'm gonna loop trough the feed and print them into a database, but since I'm not able to even get some errors out of this I fear my knowledge isn't enough for this.

I can print out the $str_xml without any problems so the file is loaded correctly.

I'm thankful for any help I can get!

Upvotes: 1

Views: 758

Answers (2)

Professor Abronsius
Professor Abronsius

Reputation: 33813

The xml was invalid and the call to simplexmlelement was using a string rather than the variable

    $str_xml='<?xml version="1.0" encoding="iso-8859-1"?>
                <products>
                  <product id="809809">
                    <name>LongJohn</name>
                    <brand>7</brand>
                    <product_url>https://www.example.com/producturl.html</product_url>
                    <image_url>https://www.example.com/product.jpg</image_url>
                    <price>369.00</price>
                    <former_price>369.00</former_price>
                    <in>Y</in>
                    <sum>110297</sum>
                  </product>
                </products>';
    $library = new SimpleXMLElement( $str_xml );
    print_r( $library );

Upvotes: 1

Michiel
Michiel

Reputation: 123

Check the php site; You provide SimpleXMLElement with a string that is not xml at all.

$str_xml = file_get_contents('pricetest1.xml');
$library = New SimpleXMLElement($str_xml);
print_r $library;

Oh; and your xml is invalid;

Upvotes: 1

Related Questions