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.
    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
    // 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 (
    // Handle Errors
    if ($error)
    die ("Error Importing People: $error");


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

       <Person id="000734" email="bob@example.com">
             <Address>123 4th St.</Address>

    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.