Solved

Create XML from class definition

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Site hacked - decoding the PHP? 15 66
PHPStorm debugging issues 1 25
PHP: concatenate query 13 45
Testing a string condition in PHP 6 10
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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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…

861 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

24 Experts available now in Live!

Get 1:1 Help Now