123beginners.com
123beginners.com

Reputation: 1

How to fetch data by id from xml using php

I'm a PHP/developing novice and I haven't been able to find a tutorial with a solution to this particular problem.

I have a XML file from an API with product information for an online store. Each physical product has multiple "styles" that need to be listed on the same page. I want to do that using their id numbers.

<products>
  <product id="001">
    <name>Shirt 1 - Women's</name>
    <price>12.00</price>
    <color>blue</color>
  </product>

  <product id="002">
    <name>Shirt 1 - Men's</name>
    <price>12.00</price>
    <color>red</color>
  </product>
   ...
  <product id="023">
    <name>Shirt 12 - Women's</name>
    <price>15.00</price>
    <color>purple</color>
  </product>

  <product id="024">
    <name>Shirt 12 - Men's</name>
    <price>15.00</price>
    <color>yellow</color>
  </product>

</products>

Upvotes: 0

Views: 314

Answers (1)

Jasper
Jasper

Reputation: 534

You could use a XSLT file to transform your XML to your HTML code. Here's some documentation, and examples, from php.net: http://php.net/manual/en/book.xsl.php

Another solution would be to fetched the data from XML with the use of XPath queries. This way you can transform your XML data to a list of PHP objects which you can then use in your templating engine. Examples and info: http://php.net/manual/en/class.domxpath.php

Upvotes: 1

Related Questions