SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: XML parsing

  1. #1
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML parsing

    I am trying to make an XML parsing function, however i can't find any good and/or helpful tutorials.

    I want to make it do pretty much the same as simpleXML what does:
    - read a file
    - parse it
    - lay it out an Array or Object (eg $bit = $xml["parent"]["child"])

    and i want all this in function.

    oh ... and i want it PHP 4 compatible.

    SO could you please point me to a good tutorial, or write the code for me (pretty please?). Thanks in advance, and thanks again when its sorted for all those that help

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unfortunately that example does not work...

  4. #4
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Posts
    1,295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use the following to generate an xml (rss) feed for my site using php 4. Works a treat,

    Alex

    <?php
    header("Content-type: text/xml");

    $dbh=mysql_connect ("localhost", "xxxx", "xxxxxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("xxxxxx");

    $query="SELECT whatever you want";
    $result=mysql_query($query);

    echo ("<?xml version=\"1.0\"?>
    <rss version=\"2.0\">
    <channel>
    <title>Feed Title</title>
    <link>Website Link</link>
    <description>Feed Description</description>");

    WHILE($row=mysql_fetch_assoc($result)) {
    echo ("
    <item>
    <title>" . $row['title] . "</title>

    <description>" . $row['description'] . "</description>

    <link>" . $row['link'] . "</link>
    <guid>". $row['guid'] . "</guid>
    </item>");
    }

    echo ("
    </channel>
    </rss>");
    ?>
    Edit:

    Just realised this isn't exactly what you asked for...but I'll leave it here anyway.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Check out the user comments in php manual[*], there are some good examples.
    Saul

  6. #6
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay , ive got it to a point where i have an array path, but in serial, like so:

    Code:
    	global $xa;
    	$rt = array();
    	$cn = $node;
    	$ins = NULL;
    	while(($cn != $root)) {
    	$rt[] = $cn->tagName;
    		$cn = $cn->parentNode;	
    	}
    	$rt = array_reverse($rt);
    this results in something like:
    Code:
    array([root]=>[parent]=>[child]=>)
    when print_r'ed

    how do add it to the array $xa?

    i tried this:
    Code:
    	foreach($rt as $key=>$value) {
    		if($value != "") { $ins .= "[".$value."]";}
    	}
                 $xa$ins = $node->value;
    but this does not work. how do i solve all of this? please help!

  7. #7
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OKay it is solved now. I used the DOM functions to sort it into an array:

    Code:
    function xmlArray($file) {
    	$doc = new domDocument();
    	$doc->load($file);
    	if($doc) {
    		$library = $doc->documentElement; 
    		$shelves = $library->childNodes; 
    		foreach ($shelves as $shelf) { 
    			if ($shelf instanceof domElement) { 
    				$xa[$shelf->tagName] = array();
    				$books = $shelf->childNodes; 
    				foreach ($books as $book) { 
    					if ($book instanceof domElement) { 
    						$xa[$shelf->tagName][$book->tagName] = $book->nodeValue;
    					} 
    				} 
    			} 
    		}
    		return $xa;
    	} else {
    		return false;
    	}
    }
    21 lines of code isnt bad, but it might be quite resource heavy. And im not sure the DOM functions are PHP 4 (DOMXML are for sure) so if they arent i will just use simpleXML

    thanks for all those who helped.

  8. #8
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I needed some function for xml parsing month ago so I modified function from manual, so it will like that

    $tree["element1"]["element2"]["$text"] which will give the value for
    <element1>
    <element2>text</element2>
    </element1>

    it can handle as stated on xml manual page different attributes, tags like <element /> and i added support to ignore xml-directives but you can easily changed it
    I KNOW ONLY ONE DRAWBACK in this function - it can't parse
    <element><element>text</element></element>(tag with the same tag inside). If you can fix it please post the fix

    --------------------------------------------
    <?php

    class Xml{
    var $s="\$";
    var $a="#";

    function parse($xml){
    $xmlary=array();

    if ((strlen($xml) < 256) && is_file($xml)){ $xml=file_get_contents ($xml); }

    $ReElements='/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<(\/\s*\1\s*)>)/s';
    $ReAttributes='/(\w+)=(?:"|\')([^"\']*)?"|\')/';

    preg_match_all($ReElements, $xml, $elements);
    foreach ($elements[1] as $ie => $xx) {
    $name=$xx;
    if(isset($xmlary[$name][0])){
    $ref= & $xmlary[$name][count($xmlary[$name])];
    }
    else{
    if(isset($xmlary[$name])){
    $temp=$xmlary[$name];
    unset($xmlary[$name]);
    $xmlary[$name][0]=$temp;
    $ref= & $xmlary[$name][1];
    }
    else{
    $ref= & $xmlary[$name];
    }
    }
    $ref[$this->s . "tag"]=$elements[1][$ie];
    if ( $attributes=trim($elements[2][$ie])) {
    preg_match_all ($ReAttributes, $attributes, $att);
    foreach ($att[1] as $ia => $xx)
    $ref[$this->a . $att[1][$ia]]=$att[2][$ia];
    } // if $attributes

    //$cdend=strpos($elements[3][$ie],"<");
    //if ($cdend > 0) {
    $ref[$this->s . "text"]=strip_tags($elements[3][$ie]);
    $ref[$this->s . "html"]=$elements[3][$ie];
    //} // if cdend

    if (preg_match ($ReElements, $elements[3][$ie])){
    foreach($this->parse($elements[3][$ie]) as $key=>$value){
    $ref[$key]=$value;
    }
    }
    else if (isset($elements[3][$ie])){
    $ref[$this->s . "text"]=$elements[3][$ie];
    $ref[$this->s . "intext"]=$elements[3][$ie];
    }
    $ref[$this->s . "closetag"]=$elements[4][$ie];
    }//foreach ?

    return $xmlary;
    }

    }

    ?>
    Attached Files Attached Files

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Php_penguin
    OKay it is solved now. I used the DOM functions to sort it into an array:

    Code:
    function xmlArray($file) {
    	$doc = new domDocument();
    	$doc->load($file);
    	if($doc) {
    		$library = $doc->documentElement; 
    		$shelves = $library->childNodes; 
    		foreach ($shelves as $shelf) { 
    			if ($shelf instanceof domElement) { 
    				$xa[$shelf->tagName] = array();
    				$books = $shelf->childNodes; 
    				foreach ($books as $book) { 
    					if ($book instanceof domElement) { 
    						$xa[$shelf->tagName][$book->tagName] = $book->nodeValue;
    					} 
    				} 
    			} 
    		}
    		return $xa;
    	} else {
    		return false;
    	}
    }
    21 lines of code isnt bad, but it might be quite resource heavy. And im not sure the DOM functions are PHP 4 (DOMXML are for sure) so if they arent i will just use simpleXML

    thanks for all those who helped.
    how to use simpleXML to sign xml as array?


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
  •