• home
  • forum
  • my
  • kt
  • download
  • Parsing XML Using PHP - Amazon Used as an Example

    Author: 2007-08-24 19:06:04 From:

    Here is one example of how to parse XML results using PHP.

    To provide a specific example, I¡¯ve created a form and code that works with Amazon¡¯s web services. The code below displays a basic form, which queries Amazon, retrieves the XML data, parses the results, and displays them in a nicely formatted table.

    Note that in order for this code to work, you will need to obtain your own Access Key ID, which you can easily do here.

    • The complete script can be found here
    • The XML sample file can be found here

    We¡¯ll begin by  a class containing the objects for our xml script:

    Next, we need to identify the starting element and the ending element.  Pay close attention the $element variable in both functions.  This means that any tag in the XML document that we reference in this script must be typed in lowercase letters.  So, if there¡¯s a tag we need to use .

    Here is where we create a new parser, tell it what to parse, and parse it.

    NOTE: The array below should be filled in with the tags from the XML document you are parsing.  Type 1 is a single field and type 2 is an array field.

    NOTE: Change ¡®item¡¯ to whatever the closing tag is from your XML document.

    In the event that one of the field_types (set in the OfferList function) is type 2, we¡¯ll tell it where to find the text:

    Here is a function to sort the results.  Note that, at least for Amazon, you can specify a parameter in the XML url to sort the results. But, if the url you¡¯ll be using doesn¡¯t have that feature, this is handy.

    And this function display the results:

    If errors were found with the user input, we¡¯ll show how to display the errors and we¡¯ll echo the form with the error messages:

    And if the user hasn¡¯t done anything yet, we¡¯ll simply display the form:

    I¡¯ve modified this code to include sort functionality and a feature that will display the XML results in a table with alternating row colors. Additionally, this code includes form validation. Much of the code has been borrowed from Programming PHP.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot