PX - Home
Grab the Latest Version Now!
  • Download the latest, px.017.php
  • Do the Quick Start tutorial.
  • Make your own Plugins!
  • Leave comments on The Forum.
  • Easy DOM-Manipulation
    The PX Library simplifies manipulation of XML documents with an intuitive set of methods that work in a way very similar to the popular jQuery JavaScript library. PX is designed to significantly speed development of DOM manipulation scripts.
    What it Is for
    The pX library is perfect for quick development of php scripts meant to import data from XML files to a database or to static HTML files, or to help you clean up the tags or attributes in HTML or XML files.
    What it's Not for
    The pX library is designed for rapid development, but is typically not ideal for dynamic content generation on busy servers.
    Syntax
    Aside from the obvious syntax differences between PHP and JavaScript, px object code looks and feels very much like the DOM Manipulation methods in jQuery. If you're familiar with those, then you're half way to working with PX.
    // Load an XML file into a px Object
    $doc = new px("people.xml");
    // Get DOM Elements Describing Each Person
    $people=$doc->xpath("/People/Person");
    // Execute a Function for Each Person
    $people->each(function($people, $iOffset) {
    // Get the Current Person
    $Person = $people->index($iOffset);
    // Pass Person Info to DB Insertion Function
    $error = db_insert_person (
    $Person->attr("id"),
    $Person->find("FullName")->text(),
    $Person->find("Mailing/Address")->text(),
    $Person->find("Mailing/City")->text(),
    $Person->find("Mailing/State")->text(),
    $Person->find("Mailing/PostalCode")->text(),
    $Person->find("Mailing/Country")->text(),
    $Person->attr("EMail")
    );
    // Handle Errors
    if ($error)
    die ("Error Importing People: $error");
    });

    Example

    The example above reads a People.xml file. An example of that file's contents is shown here:

    <People>
       <Person id="000734" email="bob@example.com">
          <FullName>Bob</Fullname>
          <Mailing>
             <Address>123 4th St.</Address>
             <City>Exampleton</City>
             <State>Florida</State>
             <PostalCode>11111</PostalCode>
             <Country>USA</Country>
          </Mailing>
       </Person>
    </People>

    The example inserts each Person tag into a database (calling a db_insert_person function, which is not shown). The following steps outline what's happening:

    1. A px object is created, passing the file name of a People.xml file. This file would contain xml describing a list of people, their names, mailing address, and Email. The email (and a unique id) would be stored as attributes.
    2. The xpath method is used to retrieve a list of the People elements, contained in the root Person element. The result is stored in a $people variable.
    3. The each method is called on the $people variable, receiving a Closure as the argument. The Closure will be called once for each Person tag within the root People tag.
    4. Within the Closure, PHP code is executed once for each Person. The function receives $people, the calling pxObject, and an integer, $iOffset, that gives access to the current child being examined.
    5. A $Person variable is set, by passing $iOffset to the index method. This variable will give more direct access to the attributes and child tags of the current Person.
    6. The db_insert_person function is called. It's parameters, which would theoretically be used to generate an SQL insert statement, are generated by calls to the $Person object's attr, find, and text methods.