Last updated 10/30/2002This is an improved version of my previous XML To Array function submitted on 09/19/02. It supports XML tag attributes and also single tag XML elements. Check back for updates to this code (see last update date above). Old Description: This function converts an xml file to an associative array. It supports multiple attributes of the same name on the same level through use of an array. It could be very useful in situations where small xml files are being used for config info or data.
By : simgar
<?php // {{{ toString() /** * This method converts a file to a string. It returns an Error object if it is unable to open the file. * * @param fileName String. The name of the file to convert. * * @return String * @author simgar */
function & toString( $fileName ) { if ($content_array = file($fileName)) { return implode("", $content_array); } else { // Error return false; } } // }}}
// {{{ xmlFileToArray() /** * This static method converts an xml file to an associative array * duplicating the xml file structure. * * @param $fileName. String. The name of the xml file to convert. * This method returns an Error object if this file does not * exist or is invalid. * @param $includeTopTag. booleal. Whether or not the topmost xml tag * should be included in the array. The default value for this is false. * @param $lowerCaseTags. boolean. Whether or not tags should be * set to lower case. Default value for this parameter is true. * @access public static * @return Associative Array * @author Jason Read <jason@ace.us.com> */ function & xmlFileToArray($fileName, $includeTopTag = false, $lowerCaseTags = true) { // Definition file not found if (!file_exists($fileName)) { // Error return false; } $p = xml_parser_create(); xml_parse_into_struct($p,toString($fileName),$vals,$index); xml_parser_free($p); $xml = array(); $levels = array(); $multipleData = array(); $prevTag = ""; $currTag = ""; $topTag = false; foreach ($vals as $val) { // Open tag if ($val["type"] == "open") { if (!_xmlFileToArrayOpen($topTag, $includeTopTag, $val, $lowerCaseTags, $levels, $prevTag, $multipleData, $xml)) { continue; } } // Close tag else if ($val["type"] == "close") { if (!_xmlFileToArrayClose($topTag, $includeTopTag, $val, $lowerCaseTags, $levels, $prevTag, $multipleData, $xml)) { continue; } } // Data tag else if ($val["type"] == "complete" && isset($val["value"])) { $loc =& $xml; foreach ($levels as $level) { $temp =& $loc[str_replace(":arr#", "", $level)]; $loc =& $temp; } $tag = $val["tag"]; if ($lowerCaseTags) { $tag = strtolower($val["tag"]); } $loc[$tag] = str_replace("\\n", "\n", $val["value"]); } // Tag without data else if ($val["type"] == "complete") { _xmlFileToArrayOpen($topTag, $includeTopTag, $val, $lowerCaseTags, $levels, $prevTag, $multipleData, $xml); _xmlFileToArrayClose($topTag, $includeTopTag, $val, $lowerCaseTags, $levels, $prevTag, $multipleData, $xml); } } return $xml; } // }}}
// {{{ _xmlFileToArrayOpen() /** * Private support function for xmlFileToArray. Handles an xml OPEN tag. * * @param $topTag. String. xmlFileToArray topTag variable * @param $includeTopTag. boolean. xmlFileToArray includeTopTag variable * @param $val. String[]. xmlFileToArray val variable * @param $currTag. String. xmlFileToArray currTag variable * @param $lowerCaseTags. boolean. xmlFileToArray lowerCaseTags variable * @param $levels. String[]. xmlFileToArray levels variable * @param $prevTag. String. xmlFileToArray prevTag variable * @param $multipleData. boolean. xmlFileToArray multipleData variable * @param $xml. String[]. xmlFileToArray xml variable * @access private static * @return boolean * @author Jason Read <jason@ace.us.com> */ function _xmlFileToArrayOpen(& $topTag, & $includeTopTag, & $val, & $lowerCaseTags, & $levels, & $prevTag, & $multipleData, & $xml) { // don't include top tag if (!$topTag && !$includeTopTag) { $topTag = $val["tag"]; return false; } $currTag = $val["tag"]; if ($lowerCaseTags) { $currTag = strtolower($val["tag"]); } $levels[] = $currTag; // Multiple items w/ same name. Convert to array. if ($prevTag === $currTag) { if (!array_key_exists($currTag, $multipleData) || !$multipleData[$currTag]["multiple"]) { $loc =& $xml; foreach ($levels as $level) { $temp =& $loc[$level]; $loc =& $temp; } $loc = array($loc); $multipleData[$currTag]["multiple"] = true; $multipleData[$currTag]["multiple_count"] = 0; } $multipleData[$currTag]["popped"] = false; $levels[] = ":arr#" . ++$multipleData[$currTag]["multiple_count"]; } else { $multipleData[$currTag]["multiple"] = false; } // Add attributes array if (array_key_exists("attributes", $val)) { $loc =& $xml; foreach ($levels as $level) { $temp =& $loc[str_replace(":arr#", "", $level)]; $loc =& $temp; } $keys = array_keys($val["attributes"]); foreach ($keys as $key) { $tag = $key; if ($lowerCaseTags) { $tag = strtolower($tag); } $loc["attributes"][$tag] = & $val["attributes"][$key]; } } return true; } // }}}
// {{{ _xmlFileToArrayClose() /** * Private support function for xmlFileToArray. Handles an xml OPEN tag. * * @param $topTag. String. xmlFileToArray topTag variable * @param $includeTopTag. boolean. xmlFileToArray includeTopTag variable * @param $val. String[]. xmlFileToArray val variable * @param $currTag. String. xmlFileToArray currTag variable * @param $lowerCaseTags. boolean. xmlFileToArray lowerCaseTags variable * @param $levels. String[]. xmlFileToArray levels variable * @param $prevTag. String. xmlFileToArray prevTag variable * @param $multipleData. boolean. xmlFileToArray multipleData variable * @param $xml. String[]. xmlFileToArray xml variable * @access private static * @return boolean * @author Jason Read <jason@ace.us.com> */ function _xmlFileToArrayClose(& $topTag, & $includeTopTag, & $val, & $lowerCaseTags, & $levels, & $prevTag, & $multipleData, & $xml) { // don't include top tag if ($topTag && !$includeTopTag && $val["tag"] == $topTag) { return false; } if ($multipleData[$currTag]["multiple"]) { $tkeys = array_reverse(array_keys($multipleData)); foreach ($tkeys as $tkey) { if ($multipleData[$tkey]["multiple"] && !$multipleData[$tkey]["popped"]) { array_pop($levels); $multipleData[$tkey]["popped"] = true; break; } else if (!$multipleData[$tkey]["multiple"]) { break; } } } $prevTag = array_pop($levels); if (strpos($prevTag, "arr#")) { $prevTag = array_pop($levels); } return true; } // }}} ?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Database Code Articles More By Codewalkers developerWorks - FREE Tools! | Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
| | | | Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
| | | | Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
| | | | Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
| | | | Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
| | | | This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
| | | | The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
| | | | The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |