Solved

Create XML from class definition

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now