<?php
/*khalid XML Files Parser:: Class Kxparse, started in March 2002 by Khalid al-kary*/
Class kxparse{
var $xml;
var $cursor;
var $cursor 2;
The constructor $xmlfile is the file with want to load into the parser
function Kxparse ($xmlfile)
{
Just read the file
$file =fopen ($xmlfile, "R");
Put the text inside the "file in the" XML object variable
while (!feof ($file))
{
$this->xml.=fread ($file, 4096);
}
Close the opened file
Fclose ($file);
Set the cursor to 0 (start of document), the cursor was later used by another functions
$this->cursor=0;
Set the second CUROSR to the end of document
$this->cursor2=strlen ($this->xml);
}
/*this function gets a copy of the XML file starting from cursor and ending with Cursor2
And then counts the number of occurences of the given tag name inside
Returns an array (occurrence index-> occurence position in the XML file)
This function is half of the engine that moves kxparse * *
function Track_tag_cursors ($tname)
{
Getting the copy as intended
$currxml =substr ($this->xml, $this->cursor, $this->cursor2);
Counting the number of occurences in the cut area
$occurs =substr_count ($currxml, "<" $tname);
The aray that'll be returned
$tag _poses=array ();
Setting its 0-0 because indeces in Kxparse start from 1
$tag _poses[0]=0;
For each of the occurences
for ($i =1; $i <= $occurs; $i + +)
{
if ($i!=1)
{
If it ' s not the ' occurence '
Start checking for the next occurence but the the previous occurences out from the string
$tag _poses[$i]=strpos ($currxml, "<" $tname, $tag _poses[$i -1]+1-$this->cursor) + $this->cursor;
}
Else
{
If its the the occurence just assign its value + the cursor (because the position the XML file wholly
$tag _poses[$i]=strpos ($currxml, "<" $tname) + $this->cursor;
}
}
Return that array
return $tag _poses;
}
This function strips and decodes the tag text ...
function Get_tag_text_internal ($tname)
{
Strip the tags from the returned text and the decode it
return $this->htmldecode (Strip_tags ($tname));
}
function that returns a particular attribute value ...
The tag is the tagged itself (with its start and end)
function get_attribute_internal ($tag, $attr)
{
Identifying the "character directly after the" tag name to cut it then
if (Strpos ($tag, "") <strpos ($tag, ">"))
{
$separ = "";
}
Else
{
$separ = ">";
}
Cutting of the tag name according to Separ
$tname =substr ($tag, 1,strpos ($tag, $separ)-1);
Cut the tagged starting from the white spaces after the tag name, ending with (not containing) the > of the tag start
$work =substr ($tag, strlen ($tname) +1,strpos ($tag, ">")-strlen ($tname)-1);
Get the index of the tag occurence inside $work
$index _of_attr=strpos ($work, "". $attr. " = "") +1;
Check if the attribute is found in the tag
if ($index _of_attr)
{
The attributename+ "=" "+attrbutevalue+" "" "and extract the value from between them
Calculate from where we'll cut
$index _of_value= $index _of_attr+strlen ($attr) +2;
Cut note the "Last argument for calculating" end
$work =substr ($work, $index _of_value,strpos ($work, "" ", $index _of_value)-$index _of_value);
Now return the attribute value
return $work;
}
If the attribute wasn ' t found, return false '
Else
{
return FALSE;
}
}
This function html-decodes the Var $text ...
function HtmlDecode ($text)
{
$text =str_replace ("<", "<", $text);
$text =str_replace (">", ">", $text);
$text =str_replace ("&", "&", $text);
$text =str_replace ("<t;", "<", $text);
$text =str_replace (">t;", ">", $text);
return $text;
}
The function that saves a file to a particular location
function Save ($file)
{
Open the file and overwrite of already avilable
$my _file=fopen ($file, "WB");
$my _status holds wether the operation is okay
$my _status=fwrite ($my _file, $this->xml);
Close the file handle
Fclose ($my _file);
if ($my _status!=-1)
{
return TRUE;
}
Else
{
return FALSE;
}
}
function that gets a tag in the XML tree (with its starting and ending)
function Get_tag_in_tree ($tname, $tindex)
{
$this->get_work_space ($tname, $tindex);
Return substr ($this->xml, $this->cursor, $this->cursor2-$this->cursor);
}
function that gets the text of a tag
function Get_tag_text ($tname, $tindex)
{
$mytag = $this->get_tag_in_tree ($tname, $tindex);
return $this->get_tag_text_internal ($mytag);
}
Funtion that counts the number of occurences to a tag in the XML tree
function Count_tag ($tname, $tindex)
{
return $this->get_work_space ($tname, $tindex);
}
Functoin that gets the attribute value in a tag
function Get_attribute ($tname, $tindex, $attrname)
{
$mytag = $this->get_tag_in_tree ($tname, $tindex);
return $this->get_attribute_internal ($mytag, $attrname);
}
Very important function, half of the engine
Sets the $this->cursor and $this->cursor2 to the place where it's intended to work
function Get_work_space ($tname, $tindex)
{
Counts the number of ":" in the given Colonedtagindex
$num _of_search=substr_count ($tindex, ":");
Counts the number of ":" in the given Colonedtagname
$num _of_search_text=substr_count ($tname, ":");
Checks if they are not equal this regarded an error
if ($num _of_search!= $num _of_search_text)
{
return false;
}
Else
{
Now get the numbers in an array
$search _array=explode (":", $tindex);
and also get the corresponding tag names
$search _text_array=explode (":", $tname);
Set the cursor to 0 in order to erase former work
$this->cursor=0;
Set the Cursor2 to the "end of" the same reason
$this->cursor2=strlen ($this->xml);
Get the The Intiate
$currtname = $search _text_array[0];
Get the The "the" the "the" "the" intiate the Loop
$currtindex = $search _array[0];
The loop according to number of ":"
for ($i =0; $i <count ($search _array); $i + +)
{
If it ' s not the ' the ' '
if ($i!=0)
{
So append the "latest colonedtagname to" the current tag name
$currtname = $currtname. ":" $search _text_array[$i];
and append the Latset colonedtagindex to the current tag index
$currtindex = $currtindex. ":" $search _array[$i];
}
$arr holds the number of occurences of the current tag name between the cursor and CURSOR2
$arr = $this->track_tag_cursors ($search _text_array[$i]);
The index which you want to the position of
$tem = $search _array[$i];
To support Count_tag_in_tree
When given a? It returns the number of occurences of the current tag name
if ($tem = = "?")
{
return count ($arr)-1;
}
else {
To support the Auto-last method
If the current tagged index equals "-1" so replace it with the last occurence index
if ($tem ==-1)
{
$tem =count ($arr)-1;
}
Now just set cursor one to the occurence position in the XML file accrding to $tem
$this->cursor= $arr [(int) $tem];
and set Cursor2 at the "End of" tag
$this->cursor2=strpos ($this->xml, "</". $search _text_array[$i]. " > ", $this->cursor) +strlen (" </". $search _text_array[$i]." > ");
}
}
}
}
The function that appends a tag to the XML tree
function Create_tag ($tname, $tindex, $ntname)
{
The intended Father tag
$this->get_work_space ($tname, $tindex);
Explode the given colonedtagname into an array
$search _text_array=explode (":", $tname);
After setting the cursors using Get_work_space
Get a cope of the returned tag
$workarea =substr ($this->xml, $this->cursor, $this->cursor2-$this->cursor);
Calculate the place where you'll put the tag, start and end
$inde = $this->cursor+strpos ($workarea, "</". $search _text_array[count ($search _text_array)-1]. " > ");
Here, replace means insert because the length argument was set to 0
$this->xml=substr_replace ($this->xml, "<" $ntname. " ></". $ntname." > ", $inde, 0);
}
The function that is sets the value of an attribute
function Set_attribute ($tname, $tindex, $attr, $value)
{
The cursors using Get_work_space
$this->get_work_space ($tname, $tindex);
Now get a copy of the XML tag between cursor and CURSOR2
$currxml =substr ($this->xml, $this->cursor, $this->cursor2-$this->cursor);
Cut the "area of" the tag on which your want to work
Starting from the tag ' < ' and ending with the opening tag ' > '
$work =substr ($currxml, 0,strpos ($currxml, ">") +1);
If the attribute is already available
if (Strpos ($work, "". $attr. " =""))
{
Calculate the current value ' s length
$currval _length=strlen ($this->get_attribute_internal ($currxml, $attr));
Get the position of the attribute inside the tag
$my _attribute_pos=strpos ($work, "". $attr. " = "") +1;
Get the length of the attribute
$my _attribute_length=strlen ($attr);
Now replace the old value
$this->xml=substr_replace ($this->xml, $value, $this->cursor+ $my _attribute_pos+ $my _attribute_length+2,$ Currval_length);
return TRUE;
}
If the attribute wasn ' t already available '
Else
{
Check if there are other attributes in the tag
if (Strpos ($work, ""))
{
$separ = "";
}
Else
{
$separ = ">";
}
Prepare the attribute
$newattr = "". $attr. = "". $value. "" ";
Insert the new attribute
$this->xml=substr_replace ($this->xml, $newattr, $this->cursor+strpos ($work, $separ), 0);
return TRUE;
}
}
The function that changes or adds the text of a tag
function Set_tag_text ($tname, $tindex, $text)
{
Firs get set the cursors using Get_work_space
$this->get_work_space ($tname, $tindex);
Explode the given colonedtagname in an array
$search _text_array=explode (":", $tname);
Get the latest name
$currtname = $search _text_array[count ($search _text_array)-1];
Calculate the start of replacement
$replace _start_index=strpos ($this->xml, ">", $this->cursor) +1;
Calculate the end of replacement
$replace _end_index=strpos ($this->xml, "</". $currtname. " > ", $this->cursor)-1;
Calculate the length between them
$tem = $replace _end_index-$replace _start_index+1;
And now replace
$this->xml=substr_replace ($this->xml, $text, $replace _start_index, $tem);
}
Functio that removes a tag
function Remove_tag ($tname, $tindex)
{
Set the cursors using Get_work_space
$this->get_work_space ($tname, $tindex);
Now replace with ""
$this->xml=substr_replace ($this->xml, "", $this->cursor, $this->cursor2-$this->cursor);
}
}
?> testing.xml 0100400 0177776 0177776 00000001125 07561460576 013104 0 ustar nobody nogroup <?xml version= "1.0"?>
<tryingxml testattribute= "We are Here" >
<firstlevel testingattribute= "No we aren ' t here" >
Sometext for the
<secondlevel testingattribute= "We are there" >
Some text for the second level
<thirdlevel testingattribute= "We aren ' t there" >
Some text for the third level
</thirdlevel>
<thirdlevel testingattribute= "We are every where:)" >
This is a test by multiple children per parent node
<justachild testingattribute= "A value ofcourse" >is it okay with you? Go check the script</justachild>
</thirdlevel>
</secondlevel>
</firstlevel>
</tryingxml>
nav.php 0100400 0177776 0177776 00000000543 07561460576 012205 0 ustar nobody nogroup <?php
including Khalid XML parser
Include_once "kxparse.php";
Create the Object
$xmlnav =new kxparse ("Nav.xml");
For ($i =1 $i <= $xmlnav->count_tag ("Nav:item", "1:?"); $i + +)
{?>
<div class= "Navit" ><a href= "<?php Echo $xmlnav->get_attribute (" Nav:item "," 1: ". $i," href ")?>" > <?php Echo $xmlnav->get_tag_text ("Nav:item", "1:". $i)?></a></div>
?? >
nav.xml 0100400 0177776 0177776 00000000507 07561460576 012216 0 ustar nobody nogroup <?xml version= "1.0"?>
<nav>
<item href= "http://111cn.net/kxparse/index.php" >Home</item>
<item href= "http://111cn.net/kxparse/news.php" >News</item>
<item href= "http://111cn.net/kxparse/docs.php" >Documentation</item>
<item href= "http://111cn.net/kxparse/changelog.php" >change log</item>
</nav>