OZONE Asylum
Forums
XML - XSL - XSLT - XHTML - CSS - DOM
XML for php: tutorial draft
This page's ID:
10822
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
Ok, hands-on! What do we have here, at the very beginning of our script? [code] $file = "data.xml"; $map_array = array( "BOLD" => "B", "EMPHASIS" => "I", "LITERAL" => "TT"); [/code] According to this, the file to parse in a "data.xml", and the tags to map are "BOLD", "EMPHASIS", "LITERAL", which respectively become "B","I" and "TT", here stored as an associative array. Remember the following line? xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true); "CASE_FOLDING", here, means uppercase, hence the uppercase tag names in our array: we set our parser to give us uppercase tag names. But how and when is this array used? Being external to all functions in script it has a global scope, and we can find it later on in: [code] function startElement($parser, $name, $attrs) { global $map_array; if ($htmltag = $map_array[$name]) { print "<$htmltag>"; } } [/code] Line 1, by declaring our map_array global, we create a reference to it inside the function. line 2, we check the $name, in map array, and see if it exists. $name is brought by the xml parser as a stripped xml tag (only the name). And the php xml_parser triggers open and close events accordingly, and depending on the handlers we set in: xml_set_element_handler($xml_parser, "startElement", "endElement"); If $name exists, the instruction in line 2 also assigns its value to $htmltag, a new variable. Weird syntax to some? but it does the trick: we're checking to see wether the expression applies or note (true/false), which also does apply the expression. Line 3, $htmltag is converted back (adding the "<>") to html and printed to output/browser. And that's about it: $attrs could be recovered as an (associative) array of names and values. Since we defined our parser for these handlers, each time it meets an openning tag, it will call the appropriate function and pass it the name and attributes of the current tag. In our main sample, we also got: [code] function characterData($parser, $data) { print $data; } [/code] Which is a tag indepedent function, in fact it simply spits out the contents of an xml tag == the value [b]between[/b] the openning and closing tag. Again, we chose this when chose an xml_parser configuration: xml_set_character_data_handler($xml_parser, "characterData"); ---------------------------------------------------------------------------------------- A quick summary: A) Create and configure the parser, using xml_parser_create(), xml_parser_set_option, xml_set_element_handler to set both openning and closing tag related functions, xml_set_character_data_handler B) Define what you want your element_handlers and character_data_handler to do, by defining functions as we already discussed and rediscussed (their names are defined by you/me in the xml_..._handler directives). C) Open a file pointer to the xml source, local or remote (file or url), and fread the file 4096 per 4096 bytes, pass the output to the xml_parser, let the parser parse. D) Free the parser and ressources, this is important. xml_parser_free($Wil..$parser). -------------------------------------------------------------------------------------- (...) (...) (...) [i]But is there more to it?[/i] So much more! A) You're free to use Flash, Java, Php or anything to parse xml. Even javascript has some accesses to some libraries. B) We should... We should evolve from this sample to a class. Well, the problem is: somebody already made and published such an evolution and the tutorial. At phpbuilder, please read it: [url=http://www.phpbuilder.com/columns/justin20000428.php3?page=2]http://www.phpbuilder.com/columns/justin20000428.php3?page=2[/url] So what could we add? Flexibility: we should be able, when calling an instance of the parser class, to select which tags should be omitted. We should be able to use tags inside complex functions, regexps, date formatting, SQL queries, composite functions (SQL+regexp+multiple uses in one xml_..._handler pass). How do we achieve that? Using arrays and objects, creating xml_behaviors objects, to hold at the same time.. wait a minute. That should be the next chapter, what do you think? Crit this attempted tutorial draft now please. [This message has been edited by InI (edited 10-14-2002).] [This message has been edited by InI (edited 10-14-2002).]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »