Solved

Create XML from class definition

Posted on 2003-10-22
5
169 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

679 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