ActiveLink PHP XML Package Tutorial Script

Thank you for trying out ActiveLink PHP XML Package. Below are simple examples on how to manipulate XML and XML documents using classes contained in AciveLink PHP XML Package.

First and foremost, let's load all classes that we will use:
// include a class-loading function
require_once("classes/include.php");

// import XML and XMLDocument classes
import("org.active-link.xml.XML");
import("org.active-link.xml.XMLDocument");

EXAMPLE 1

In the first example, let's construct a simple XML tree from scratch. Let's make the root element called PREFERENCES and store some user preferences in XML format. First, let's create an XML object $myXML by:

$myXML = new XML("PREFERENCES");
This creates an XML object and assigns PREFERENCES as its root tag name. Now, let's add couple of sub-branches (child tags) under the root tag, let's say COLORSCHEME and ICONS. This is easily accomplished by calling setTagContent method as follows:
$myXML->setTagContent("Blue", "PREFERENCES/COLORSCHEME");
$myXML->setTagContent("Motif", "PREFERENCES/ICONS");
setTagContent method takes the supplied tag path and either searches and replaces existing tag contents, or, as in this case, creates new set of tags. Notice that we set "Blue" and "Motif" as tag contents of respective branch (children) tags.

Next, let's add another branch (child tag) NEWS, but now let's also add a grandchild tag also and let's call it HEADLINES.
$myXML->setTagContent("", "PREFERENCES/NEWS/HEADLINES");
Notice that, just like before, by specifying the tagpath setTagContent in this case creates a new set of tags. The tag path can be as deep as necessary, or left unspecified to assume current context.

Here is the final output from our $myXML object at the end of example 1:
<PREFERENCES />


EXAMPLE 2

In the second example, let's perform a slightly more complex manipulation of our $myXML object that we created in example 1. Under HEADLINES let's add three "item" branches (child tags) all with different contents. But this time, let's use a different method - let's first create 3 item branches as separate unassociated objects and then add them under HEADLINES in the XML tree.

$headline1 = new XMLBranch("item");
$headline2 = new XMLBranch("item");
$headline3 = new XMLBranch("item");
The above syntax should look familiar. XMLBranch class extends XML class, so all methods present in XML also are available under XMLBranch and work exactly the same way. So, let's assign new contents to each of our new branches.
$headline1->setTagContent("headline 1 content");
$headline2->setTagContent("headline 2 content");
$headline3->setTagContent("headline 3 content");
To complicate things a little more, let's set tag "id" attributes to each of the created branches so that they are easy to locate and identify. Let's assign each of their IDs as 1, 2, and 3 respectively.
$headline1->setTagAttribute("id", "1");
$headline2->setTagAttribute("id", "2");
$headline3->setTagAttribute("id", "3");
Now these branches are ready to be added to our $myXML tree.