SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML parsing is driving me bonkers

    So I spent the last two hours trying to understand how PHP parses XML. I can't get my head around it so I'm gonna keep this one brief and hopefully someone here will be kind enough to help me when providing a solution as I don't intend to understand it today

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <options>
    	<optiongroup name="Site Control" image="someimage.gif">
    		<link href="asdf">Link here</link>
    		<link href="asdf">Something</link>
    	</optiongroup>
    	<optiongroup name="News Manager" image="someimage2.gif">
    		<link href="asdf">Link here 2</link>
    		<link href="asdf">Something 2</link>
    	</optiongroup>
    </options>
    That's a sample XML file I have. I need to use the data to generate it into an array which will be in the format:

    Code:
    [0] = > array(
    	'name' => 'name of the option group here',
    	'image' => 'image attribute's value goes here',
    	'links' => array(
    		'Link name here' => 'its href value here',
    		'Link name here' => 'its href value here',
    	)
    )
    Hopefully someone can cough up something really simple for me. It'd be much appreciated

    Edit:

    Ok i'm getting somewhere now, bare with me, feel free to help in the meantime

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a couple of different ways you can do this. One is with brute force and regular expressions. Here is an example in action from the XML and Web Services forum.

    Another way, and I confess I haven't tried it but it looks interesting, is to use the XML parser from the manual. That might be a cleaner, slightly easier way of doing it once you get past the learning curve. I'd be happy to dive in and learn that method with you if you want.

    Hope this helps.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Thanks for replying! I've just managed to figure out what's going on with the PHP I'm using. But I'm having a major meltdown over why this code doesn't work. I feel like such an idiot right now, but am I missing something majorly basic here. I've used this function, for this purpose, I can't say how many times !

    PHP Code:
        $name 'optionsomg123';
        if(
    strpos($name'options'))
        {
            echo 
    '<h1>Options</h1>';
        } 

  4. #4
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you probably want something like this instead:

    Code:
    $name = 'optionsomg123'; 
        if(substr_count($name, 'options') > 0) 
        { 
            echo '<h1>Options</h1>'; 
        }
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  5. #5
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That does the trick, in the end I chose to use preg_match for different reasons I still can't figure out why strpos didn't do it's job. I've used that function so many times as illustrated above.

  6. #6
    SitePoint Zealot ceefour's Avatar
    Join Date
    Feb 2005
    Location
    Bandung, Indonesia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Use SimpleXML

    Hi Dean...

    The easiest way would be to use XML_Tree from PEAR:
    http://pear.php.net/package/XML_Tree

    Note that the latest beta version of XML_Tree treats whitespaces as whitespace, but the earlier version ignores whitespace.

    BTW if you can use PHP 5 by all means USE IT! Just take a look at SimpleXML and you'll save your asses for YEARS handling XML. Believe me. (the new DOM parser is good too)


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
  •