LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-15-2005, 08:11 AM   #1
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
php4: avoiding eval()


hey there,

i have written a little app to parse simple xml files into a php array,
problem is that i use eval() quite a lot. does anybody know a place where i can read up on how to avoid using eval() ?
thank you
 
Old 10-15-2005, 09:11 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Sounds like you wrote your own code to parse the XML, and use eval() to read tag-attributes.

If this correct, you'd be better off, using PHP's XML-parser function's. See: http://php.net/xml
 
Old 10-15-2005, 10:27 AM   #3
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Original Poster
Rep: Reputation: 32
perhaps i am not looking properly, but i was unable to find functions that would do following:
the following xml data should be truned into an array listed below the xml data
Code:
<config>
   <test>
      <blah1 type="int">1</blah1>
      <blah2>2</blah2>
      <blah3 type="string">one</blah3>
      <blah4>two</blah4>
      <blah5 type="bool">true</blah5>
      <blah6>false</blah6>
   </test>
</config>
print_r($config) output:
Code:
Array
(
    [test] => Array
        (
            [blah1] => 1
            [blah2] => 2
            [blah3] => one
            [blah4] => two
            [blah5] => 1
            [blah6] => 
        )

)
this works with as many levels as wanted, but the downside is the use of the eval() function.
the 'type' attributes just set the type of data we are using (quite obviouse...)
but the parsing process also checks when 'type' is not set whit which kind of data we are working (integer, bool, string)

i do not use eval for attributes. i read the xml document and generate a string which gets evaled...

if you can tell me which xml functions to use to get to the same place i would greatfull.

sorry if i am missing something on the page you linked, i had a look there for quite some time, but cannot see the array outputed the same way!


code: http://test.dihedral.de/xml-parsing.phps
xml: http://test.dihedral.de/xml-parsing.xml
output: http://test.dihedral.de/index.php

Last edited by Nathanael; 10-15-2005 at 10:35 AM.
 
Old 10-16-2005, 06:39 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
I'd try to do somethin like this:
PHP Code:
<html>
<head>
    <title>Config file</title>
</head>
<body>

<?php

$conf 
= array();
$tagname '';
$type '';

function 
parseTagStart($parser$name$attrs)
{
    global 
$tagname$type;
    
$tagname $name;
    
$type $attrs['TYPE'];
}

function 
parseTagEnd($parser$name) {    /* dummy */ }

function 
parseText($parser$data
{
    global 
$conf$tagname;    
    if ((
$data trim($data)) == '' ) return;
    if (
$type == 'bool'$conf[$tagname] = bool($data);
    elseif (
$type == 'int'$conf[$tagname] = int($data);
    else 
/* string? */  $conf[$tagname] = trim($data);
}

/* Read and parse the xml config file */
$filename './config.xml';
$fp fopen($filename'r') or die('Could not open xml file.');
$buf fread($fpfilesize($filename));
fclose($fp);
$P xml_parser_create() or die('Could not create XML-parser.');
xml_set_element_handler($P'parseTagStart''parseTagEnd');
xml_set_character_data_handler($P'parseText');
xml_parse($P$buf);
xml_parser_free($P);

/* The array is now populate with the data from the xml file. */

/* Just print it out */
foreach ($conf as $key => $value) {
    print 
$key .' = '$value ."<br />\n";
}
?>
</body>
</html>

Last edited by Hko; 10-16-2005 at 06:48 PM.
 
Old 10-17-2005, 04:35 AM   #5
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Original Poster
Rep: Reputation: 32
this does not create an array representing the xml file as detailed as my app. i am not looking for a replacement app, that works half as detailed, i am looking for a way to avoid the eval() function.
dont get me wrong here, i appreciate the time and effort you are putting into helping me. all i am saying that this is not quite what i looking for.
 
Old 10-17-2005, 06:00 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
That's true.
But I still think it is a code example that shows how to do such thing. Which is what a programming forum is for IMHO, as opposed to expecting other people to write your program for you entirely.

Last edited by Hko; 10-17-2005 at 06:01 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help loading php4-gd with php4 already installed. (Debian) sdduuuude Linux - Software 3 10-24-2005 05:13 AM
Avoiding rewind & eject consty Linux - General 7 06-07-2005 07:24 AM
Avoiding pitfalls... tracedroute Slackware - Installation 6 05-09-2004 04:54 PM
monolithic kernel, avoiding lkms? m00 Linux - Security 3 11-11-2003 02:08 AM
Disconnecting a directory, and avoiding this problem. wallaba Linux - Newbie 1 12-23-2002 01:11 AM


All times are GMT -5. The time now is 05:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration