SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML settings files

    Hello,

    I'm developing a settings class to handle an xml settings file, but I'm having a few issues.

    Currently I have a get method that looks like this

    PHP Code:
        public function get($key)
        {
            
    $parts explode('.'$key);
            
    $xmlFile $parts[0];
            unset(
    $parts[0]);
            if(!isset(
    $this->_settings[$xmlFile])) {
                if(!
    $this->_load($xmlFile)) {
                    return 
    null;
                }
            }
            
            
    $value $this->_settings[$xmlFile];
            
            foreach(
    $parts as $part) {
                
    $value $value->{$part};
            }

            return 
    $value;
        } 
    My xml file looks something like

    HTML Code:
    <?xml version="1.0" encoding="utf-8"?>
    <settings>
        <site>
            <meta>
                <title>A Website</title>
                <description>A really cool website.</description>
                <keywords>web, website, internet, etc</keywords>
            </meta>
        </site>
    </settings>
    And i can get values like this

    PHP Code:
    $settings->get('main.site.meta.title'); 
    The problem is that i can't think of anyway of editing these values.
    I could prod do it with eval, something like this.

    PHP Code:
    $parts explode('.'$key);
    $depth implode('->'$parts);

    eval(
    '$this->_settings[$xmlFile]->'.$depth.' = $value'); 
    But really don't like that.

    Any help would be great.

    Best Regards, George

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's easy, just load the xml into simpleXML class, creating simpleXML object
    then you can edit any value inside that object and then re-save the object as xml

    you can also use DomDocument instead of SimpleXML if you like to use DOM methods like getElementsByTagName()

    Basically the key here is to create an object that represents the xml file, then edit values inside the object and then re-save as XML file.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your response.

    $this->_settings[$xmlFile] is a simplexml object, instantiated by $this->_load($xmlFile)

    All settings are accessed like 'main.site.meta.title' where main is the file e.g. main.xml.

    I need to use this interface as it's what the client has requested.

    So what i need is a way of taking that 'main.site.meta.title' string and using it on a simplexml object like $xml->site->meta->title. I can do this with eval but don't really want to go down that route.

    Best Regards, George

  4. #4
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any particular reason you're using xml.

    If it's up to you what type of settings file you use I suggest ini file. It doesnt always give you the sophistication of xml, but you can use some tricks to get it to do what you want.

    http://us.php.net/manual/en/function.parse-ini-file.php
    http://us.php.net/manual/en/function...ini-string.php

  5. #5
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    baileylo, thanks for the suggestion, but i need to use xml for this project.

    Best Regards, George

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GOPalmer View Post
    Thanks for your response.

    $this->_settings[$xmlFile] is a simplexml object, instantiated by $this->_load($xmlFile)

    All settings are accessed like 'main.site.meta.title' where main is the file e.g. main.xml.

    I need to use this interface as it's what the client has requested.
    So your client wants to have a feel of java while using php. Why not use java then?

    Anyway, you should probably consider using the magic __set($name, $val) method

    Inside this method you can break up the string $name by dots '.' then find the actual property and then change its value to $val
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use XPath nstead?

    PHP Code:
    public function get($path)
    {
        
    $path str_replace(".""/"$path);
        
    // do not know hwo you code it, so i assume
        // that $this is instance of SimpleXmlElement
        
    return $this->xpath($path);
    }

    $settings->get('main.site.meta.title'); 
    Last edited by Greg Winiarski; Oct 27, 2009 at 00:24. Reason: missing [/php]

  8. #8
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Inside this method you can break up the string $name by dots '.' then find the actual property and then change its value to $val
    Thats where the issue lies. How do i convert a string separated by dots to a simplexml property call?

    Greg Winiarski, thanks for your suggestion, xpath seems a much more elegant solution for geting values. Though i am still unable to set values with the "dotted string" interface.

    Best Regards, George


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •