Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create XML from class definition

Posted on 2003-10-22
5
Medium Priority
?
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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