Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function xml_to_array($xml){//0JDQstGC0L7RgDoKSUNROjU1NTc3Nzk4MyBza3lwZTphb2xucDE=
- $parser = xml_parser_create();
- xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
- xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
- xml_parse_into_struct($parser, $xml, $tags);
- xml_parser_free($parser);
- $elements=array();
- $uk=&$elements;
- foreach ($tags as $tag) {
- if ($tag['type'] == "complete" || $tag['type'] == "open"){
- if(($tag['type'] == "complete")||($tag['value'])){
- $uk[$tag['tag']][]=$tag['value'];
- }
- if ($tag['type'] == "open"){
- $parent[$tag['level']]=&$uk;
- $uk=&$uk[$tag['tag']];}
- }
- if ($tag['type'] == "close"){
- $uk=&$parent[$tag['level']];
- }
- }
- return $elements;
- }
- //2 v
- function xml_to_array($xml) {
- $parser = xml_parser_create();
- xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
- xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
- xml_parse_into_struct($parser, $xml, $tags);
- xml_parser_free($parser);
- $elements = array(); // the currently filling [child] XmlElement array
- $stack = array();
- foreach ($tags as $tag) {
- $index = count($elements);
- if ($tag['type'] == "complete" || $tag['type'] == "open") {
- $elements[$index]['name'] = $tag['tag'];
- if(isset($tag['attributes']))$elements[$index]['attributes'] = $tag['attributes'];
- if(isset($tag['value']))$elements[$index]['content'] = $tag['value'];
- if ($tag['type'] == "open") { // push
- $elements[$index]['children'] = array();
- $stack[count($stack)] = &$elements;
- $elements = &$elements[$index]['children'];
- }
- }
- if ($tag['type'] == "close") { // pop
- $elements = &$stack[count($stack) - 1];
- unset($stack[count($stack) - 1]);
- }
- }
- return $elements; // the single top-level element
- }
- //3
- $xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
- $json = json_encode($xml);
- $array = json_decode($json,TRUE);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement