note 58046 added to ref.xml
| From: | aerik at wikidweb dot com | Date: | Sat, 22 Oct 2005 09:30:24 +0000 |
| Subject: | note 58046 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97406@lists.php.net to get a copy of this message | ||
For anyone else looking for a xml parser not requiring a library, here's a modified version of
xml2array, which returns a nested associative array, including attributes and nested nodes. (BTW,
thanks to whoever originally wrote these regexes - they're quite clever)
<?
function xml2array ($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) {
$xmlary[$ie]["name"] = $elements[1][$ie];
if ( $attributes = trim($elements[2][$ie])) {
preg_match_all ($ReAttributes, $attributes, $att);
foreach ($att[1] as $ia => $xx)
// all the attributes for current element are added here
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
} // if $attributes
// get text if it's combined with sub elements
$cdend = strpos($elements[3][$ie],"<");
if ($cdend > 0) {
$xmlary[$ie]["text"] = substr($elements[3][$ie],0,$cdend -1);
} // if cdend
if (preg_match ($ReElements, $elements[3][$ie]))
$xmlary[$ie]["elements"] = xml2array ($elements[3][$ie]);
else if ($elements[3][$ie]){
$xmlary[$ie]["text"] = $elements[3][$ie];
}
}
return $xmlary;
}
?>
Parsing this:
<XML>
<header>
<title> Sample App </title>
<version> v. 1.0</version>
</header>
<window height='220' width='420'>
stuff in the window
</window>
</XML>
returns a print_r of this:
Array
(
[0] => Array
(
[name] => XML
[text] =>
[elements] => Array
(
[0] => Array
(
[name] => header
[text] =>
[elements] => Array
(
[0] => Array
(
[name] => title
[text] => Sample App
)
[1] => Array
(
[name] => version
[text] => v. 1.0
)
)
)
[1] => Array
(
[name] => window
[attributes] => Array
(
[height] => 220
[width] => 420
)
[text] =>
stuff in the window
)
)
)
)
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58046
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58046&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58046&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58046&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58046&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58046&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58046&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58046&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58046&report=yes
Search -- http://master.php.net/manage/user-notes.php