note 43925 added to function.xml-parse
| From: | talraith at withouthonor dot com | Date: | Thu, 08 Jul 2004 20:40:36 +0000 |
| Subject: | note 43925 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72893@lists.php.net to get a copy of this message | ||
I have written a module that contains a class for use with XML documents. The module is
dual-purpose in that it will parse XML code into a native object tree structure and will generate
XML code from the object tree structure.
The output produced from generating XML code is designed to be used by other applications and is not
in human-readable form.
I would like to point out that the code does not use any eval() statements to create the tree.
I am posting my code for two purposes:
1) I am looking to refine it and make it more efficient
2) It may benefit someone that is looking for a module like this.
The code is too big to post in here, so I have uploaded it to a web site: http://www.withouthonor.com/obj_xml.html
An example of parsing an XML document:
<?php
// Use the method of your choice to load the XML document
// into a variable... (a very generic sample follows)
$XML = '<root><section>This is my sample XML
code</section></root>';
$xml = new xml_doc($XML);
$xml->parse();
// Access root level object
$my_tag = $xml->getTag(0,$name,$attributes,$cdata,$children);
?>
It is then possible to loop through the children of the tag and process the data with your program.
The last variable above ($children) contains a list of tag reference ID's. The object tree is
created by assigning each tag a unique ID starting with zero. The tree is created by using object
references to relate parents and children.
If you wanted to create an XML document from scratch, the code would be similar to the following
example:
<?php
$xml = new xml_doc();
// Create the root tag and retrieve reference ID #
$root_tag = $xml->createTag('root');
// The second parameter is for tag attributes
// This is done using an associative array
$xml->createTag('section',array(),'This is my sample XML code',$root_tag);
$my_output = $xml->generate();
print $my_output;
?>
The example above creates an XML document that is the same as the one used in my first example.
Another option available would be to load the XML code as in the first example, change it through
PHP, and then generate the code and output it.
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43925
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43925&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43925&report=yes
Search -- http://master.php.net/manage/user-notes.php