Solved

Create XML from class definition

Posted on 2003-10-22
5
168 Views
Last Modified: 2006-11-17
Hi,

Is it possible to read objects/class definition and variable values dynamicly and create XML file.

What I had in mind is to create some XML builder that would accept object and create XML file from it. Can it be done?
0
Comment
Question by:hpet
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:inq123
ID: 9597899
Hi hpet,

Is this for SOAP?  Regardless, you can probably use/modify code here and get what you want:

http://www.phpbuilder.com/columns/jesus20000402.php3?page=3

Cheers!
0
 
LVL 11

Expert Comment

by:shmert
ID: 9600038
You can do lots of interesting source code analysis with the tokenize function in PHP (http://php.net/tokenize).  It analyzes PHP source code and returns a big array containing all the components that make up a source file.

Be sure to check out the list of parser tokens:  http://php.net/manual/en/tokens.php

It won't be easy, but you should be able to get all the methods and variables for a php class definition.
0
 
LVL 11

Expert Comment

by:shmert
ID: 9600070
Here's the root of a documentation generator I did, should steer you in the right direction.  The whole thing is a bit too long to post here, though, so this is more for illustrative purposes.

        /**
        * Parse any classes contained in <code>$file</code>
        * @param string $file The path to the file which contains the PHP code to be parsed.
        */
        function parse($file) {
                $this->reset();
                $string = file_get_contents($file);
                if (empty($string)) die("Failed to load $file");
                $this->_tokens = token_get_all($string);
                foreach($this->_tokens AS $index=>$value) {
                        switch($value[0]) {
                                case '{':
                                        $this->_braceDepth++;
                                        break;
                                case '}':
                                        $this->_braceDepth--;
                                        break;
                                case T_COMMENT:
                                case T_ML_COMMENT:
                                        $this->_lastParsedCommentIndex = $index;
                                        break;
                                case T_CLASS:
                                        $this->_didFindClass($index);
                                        break;
                                case T_VAR:
                                        $this->_didFindVar($index);
                                        break;
                                case T_FUNCTION:
                                        $this->_didFindFunction($index);
                                        break;
                        }
                }
        }
0
 
LVL 1

Author Comment

by:hpet
ID: 9604717
I did some php function searching and I think I ran across two or three interesting functions under classes/objects area.

One is important for me:
get_object_vars();

which returns associative array of object properties and its coresponding values.

I would like to create some generic XML builder for web application I am working on. This means that I could pass various objects to it and it would output xml.
This is first time I am stepping into XML area (shame on me) but this is my "wild" idea.

Since none proposed this, do you see and problems I could ran on if I go off using it this way?

In a mean time I will examine tokens.
0
 
LVL 8

Accepted Solution

by:
inq123 earned 50 total points
ID: 9605828
>  that I could pass various objects to it and it would output xml.

That's exactly what I recommended would work.  shmert's suggestion is for source code analysis, but what I suggested would do what you want, which is really object serialization to xml.  Check out the link I gave you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checkout Page Input Field not aligned 1 26
Split wordpress loop 35 56
XML extra information 8 29
PHP and JQuery Syntax question 4 26
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question