WebReference.com - Part 4 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (2/5) | WebReference

WebReference.com - Part 4 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (2/5)

To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Professional PHP4 XML, Chapter 10: Putting It Together

Manual Writing

Manual writing is a simple and intuitive way of writing XML documents - create a string with the desired XML. Simple PHP string management is used to generate the XML file. Generating the XML manually implies that the writing function must generate well-formed XML data. We will have to parse the strings and convert special characters such as & to the proper XML entities that represent them.

For our example we'll be using the following class that parses the .txt file with the to-do list to generate an array:

class ParseToDoTxt 
{
    var $persons = Array();
    function ParseFile($filename) 
    {
        $f = fopen($filename, "r");
        if (!$f) {
            return 0;
        }
        $txt = fread($f, filesize($filename));
        return $this->ParseString($txt);
    }
    function GetPersons() 
    {
        return $this->persons;
    }
    function ParseString($txt) 
    {
        $lines = Array();
        $line = Array();
        $lines = explode("\n", $txt);
        foreach ($lines as $line) {
            $line = chop($line);
            $line = explode(",", $line);
            if (!isset($this->persons[$line[0]])) {
                $this->persons[$line[0]] = Array();
            }
            $this->persons[$line[0]][] = $line[1];
        }
        return 1;
    }
}

The following bit of code generates the XML file:

$parser = new ParseToDoTxt();
$parser->ParseFile("todo.txt");
$persons = $parser->GetPersons();
$xml = '<?xml version="1.0"?>';
$xml .= '<to-do>';
foreach ($persons as $person => $tasks) {
    $xml .= '<person>';
    $xml .= '<name>'.XmlEntities($person).'</name>';
    $xml .= '<tasks>';
    foreach ($tasks as $task) {
        $xml .= '<task>';
        $xml .= XmlEntities($task);
        $xml .= '</task>';
    }
    $xml .= '</tasks>';
    $xml .= '</person>';
}
$xml .= '</to-do>';
print ($xml);

The XmlEntities() function replaces special characters with their corresponding XML entities:

function XmlEntities($data) 
{
    $position = 0;
    $length = strlen($data);
    $escapeddata = "";
    for(;$position<$length;) {
        $character = substr($data, $position, 1);
        $code = Ord($character);
        switch($code) {
            case 34:
            $character = """;
            break;
            case 38:
            $character = "&";
            break;
            case 39:
            $character = "'";
            break;
            case 60:
            $character = "<";
            break;
            case 62:
            $character = ">";
            break;
            default:
            if ($code<32)
                $character = ("".strval($code).";");
            break;
        }
        $escapeddata .= $character;
        $position++;
    }
    return $escapeddata;
}

We can use this code as a method of the parsing class, or build a new XmlWriter class that uses the parsing class and constructs an XML string storing it as a property of the object. Without worrying about where we use the code we can see that the code is easy to understand and fairly easy to write too.

Manual writing is a simple and flexible way to write XML, however, such documents are difficult to maintain as encoding entities and producing well-formed XML documents is 100% the responsibility of the program.


To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Created: September 3, 2002
Revised: September 3, 2002

URL: https://webreference.com/programming/php/php4xml/chap10/4/2.html