root / phpframework / trunk / udf_XMLUtils.php

Revision 340, 3.8 kB (checked in by scorfield, 2 years ago)

Initial import of Fusebox 5.0.0 for PHP.

  • Property svn:executable set to *
Line 
1<?php
2function GetChildren($vals, &$i, $parentXPath) {
3
4   $children = array();
5
6   while (++$i < sizeof($vals)) {
7
8       // compair type
9       switch ($vals[$i]['type']) {
10
11           case 'cdata':
12               $children[] = $vals[$i]['value'];
13               break;
14           case 'complete':
15               $children[] = array(
16                   'xmlName' => $vals[$i]['tag'],
17                   'xmlAttributes' => getAttributes($vals, $i),
18                   'xmlValue' => getValue($vals, $i),
19                   'xmlChildren' => array(),
20                   'xmlPath' => $parentXPath.'/'.$vals[$i]['tag']
21               );
22               break;
23           case 'open':
24               $children[] = array(
25                   'xmlName' => $vals[$i]['tag'],
26                   'xmlAttributes' => getAttributes($vals, $i),
27                   'xmlValue' => getValue($vals, $i),
28                   'xmlChildren' => GetChildren($vals, $i, $parentXPath.'/'.$vals[$i]['tag']),
29                   'xmlPath' => $parentXPath.'/'.$vals[$i]['tag']
30               );       
31               break;
32           case 'close':
33               return $children;
34       }
35   }
36}
37
38function getAttributes($vals, &$i){
39    $attributes = array();
40    if ( array_key_exists("attributes",$vals[$i]) ) {
41        $attributes = $vals[$i]["attributes"];
42    }
43    return $attributes;
44}
45
46function getValue($vals, &$i){
47    $value = "";
48    if ( array_key_exists("value",$vals[$i]) ) {
49        $value = $vals[$i]["value"];
50    }
51    return $value;
52}
53
54function xmlParse($data, $enc, $bList="") {
55
56   $bArray = array();
57    // if any attributes were passed to the function, add them to the array
58    if ( strlen($bList) > 0 ) $bArray = explode(",",$bList);
59   
60    do {
61       
62        $okay = false;
63        // by: waldo@wh-e.com - trim space around tags not within
64        $data = eregi_replace(">"."[[:space:]]+"."<","><",$data);
65       
66        // XML functions
67        if ( false == ( $p = @xml_parser_create(strtoupper($enc)) ) ) break;
68       
69        // by: anony@mous.com - meets XML 1.0 specification
70        if ( false == ( @xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0) ) ) break;
71        if (false == ( @xml_parse_into_struct($p, $data, $vals, $index) ) ) break;
72        if ( false == ( @xml_parser_free($p) ) ) break;
73        $okay = true;
74       
75   } while ( false );
76   if ( $okay ) {
77
78        for ( $x = 0 ; $x < count($vals) ; $x++ ) {
79            if ( array_key_exists("attributes",$vals[$x]) ) {
80                foreach ( $vals[$x]["attributes"] as $thiskey=>$thisvalue ) {
81                   
82                    // if the attribute name exists in the "bList" then re-cast the string to a boolean
83                    if ( ( is_string($thisvalue) && array_search($thiskey,$bArray) !== false ) && ( strtolower($thisvalue) == "true" || strtolower($thisvalue) == "false" ) ) {
84                        $vals[$x]["attributes"][$thiskey] = (strtolower($thisvalue)=="true");
85                    }
86                }
87            }
88        }
89       
90       $i = 0;
91       
92       $tree["xmlChildren"] = array();
93       $tree["xmlChildren"][] = array(
94           'xmlName' => $vals[$i]['tag'],
95           'xmlAttributes' => getAttributes($vals, $i),
96           'xmlValue' => getValue($vals, $i),
97           'xmlChildren' => GetChildren($vals, $i, '//'.$vals[$i]['tag']),
98           'xmlPath' => '//'.$vals[$i]['tag']
99       );
100        $tree['xmlRoot'] = $tree['xmlChildren'][0];
101        unset($tree['xmlChildren']);
102       return $tree;
103   } else return false;
104}
105
106function xmlSearch($xmlNode,$match, $clearRet = false) {
107    static $ret = array();
108    if ( $clearRet ) $ret = array();
109    if ( isset($xmlNode['xmlRoot']) ) $xmlNode = $xmlNode['xmlRoot'];
110    if ( isset($xmlNode['xmlPath']) && $xmlNode['xmlPath'] == $match ) {
111        $ret[] = $xmlNode;
112    }
113    if ( isset($xmlNode['xmlChildren']) && count($xmlNode['xmlChildren']) > 0 ) {
114        for ( $n = 0 ; $n < count($xmlNode['xmlChildren']) ; $n++ ) {
115            xmlSearch($xmlNode['xmlChildren'][$n],$match);
116        }
117    }
118    return $ret;
119}
120
121//=============================================================+
122?>
Note: See TracBrowser for help on using the browser.