Jump to content

XML to Array, Help please

- - - - -

  • Please log in to reply
5 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hi guys,

I need help, I want convert XML to Array using php

How to XML to Array?


this is my xml code:



<?xml version="1.0" encoding="UTF-8" ?>

<software id="969">

    <waxloop id="14390385" date="20010421" time="1821">

        <description>

            <name id="Dark" price="95000">LC1</name>DHCL

        </description>

        <numc id="0"/>

        <packages>

            <package min="230" max="329751">One</package>

            <package min="305" max="329751">Two</package>

            <package min="280" max="329747">Three</package>

        </packages>

    </waxloop>


    <waxloop id="14390384" date="20010921" time="1551">

        <description>

            <name id="Dark" price="36000">LC2</name>DH8

        </description>

        <numc id="2"/>

        <packages>

            <package min="155" max="329752">One</package>

            <package min="340" max="329752">Two</package>

            <package min="540" max="329748">Three</package>

        </packages>

    </waxloop>


    <waxloop id="14390386" date="20010413" time="1011">

        <description>

            <name id="Light" price="9800">LC3</name>CD5

        </description>

        <numc id="3"/>

        <packages>

            <package min="235" max="329753">One</package>

            <package min="310" max="329753">Two</package>

            <package min="270" max="329750">Three</package>

        </packages>

    </waxloop>


    <waxloop id="13997990" date="20010518" time="1587">

        <description>

            <name id="Light" price="12000">LC4</name>CLI8

        </description>

        <numc id="4"/>

        <packages>

            <package min="122" max="1190">One</package>

            <package min="575" max="1190">Two</package>

            <package min="1300" max="171891">Three</package>

        </packages>

    </waxloop>

</software>



i need create array with php

in my array can not see description text

my description texts is:
DHCL
DH8
CD5
CLI8



Please help me


thank you so much.

Edited by lol33d, 07 August 2011 - 01:41 PM.


#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Have you tried using SimpleXML?

#3
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
yes
but i cant get
min and max in packages
<package min="230" max="329751">One</package> 

help me for this problem

thank you

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
They would be attributes. If you post some code I may be able to help you more.

#5
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
<?php

$xml_data = file_get_contents('software.xml');


$xml = new SimpleXMLElement($xml_data);


echo "<pre>";

print_r($xml);

echo "</pre>"; 

?>


---------- Post added at 09:07 AM ---------- Previous post was at 08:01 AM ----------

hi

this code for extract all attrib and data

but i cant get with array

please check it

class XMLParser

{

	var $parser;


	function XMLParser()

	{

		$this->parser = xml_parser_create();

		xml_set_object($this->parser, $this);

		xml_set_element_handler($this->parser, "startElement", "stopElement"); 

		xml_set_character_data_handler($this->parser, "characterData"); 

		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);

	}


	function startElement($parser, $name, $attrs)

	{

		echo "Start [$name] -> Attributes = ";

		print_r($attrs);

	}


	function stopElement($parser, $name)

	{

		echo "End [$name] \n";

	}


	function characterData($parser, $data)

	{

		echo "Data: $data \n";

	}


	function parse($xml)

	{

		if(!xml_parse($this->parser, str_replace(array("\n", "\r", "\t"), "", $xml)))

			echo xml_error_string(xml_get_error_code($this->parser));

		xml_parser_free($this->parser);

	}

}


#6
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
hi john

Please check this code:


<?php


class Xml2Array

{

    /**

     * XML Dom instance

     *

     * @var XML DOM Instance

     */

    private $xml_dom;

      

    /**

     * Array representing xml

     *

     * @var array

     */

    private $xml_array;

     

    /**

      * XML data

      *

      * @var String

      */

      private $xml;  

      

      

      public function __construct($xml = '')

      {

          $this->xml = $xml;

      }

      

      public function setXml($xml)

      {

          if(!empty($xml))

          {

              $this->xml = $xml;    

          }

      }

      

    /**

     * Change xml data-to-array

     * 

     * @return Array

     */

    public function get_array()

    {

        if($this->get_dom() === false)

        {

            return false;

        }

        

        $this->xml_array = array();

        $root_element = $this->xml_dom->firstChild;

        $this->xml_array[$root_element->tagName] = $this->node_2_array($root_element);

        

        return $this->xml_array;

    }

    

    private function node_2_array($dom_element)

    {

        if($dom_element->nodeType != XML_ELEMENT_NODE)

        {

            return false;

        }

        

        $children = $dom_element->childNodes;

        

        foreach($children as $child)

        {

            if($child->nodeType != XML_ELEMENT_NODE)

            {

                continue;

            }

            

            $prefix = ($child->prefix) ? $child->prefix.':' : '';

            

            if(!is_array($result[$prefix.$child->nodeName]))

            {

                $subnode = false;

    

                foreach($children as $test_node)

                {

                    if($child->nodeName == $test_node->nodeName && !$child->isSameNode($test_node))

                    {

                        $subnode = true;

                        break;

                    }

                }

            }

            else

            {

                $subnode = true;

            }

            

            if ($subnode)

            {

                $result[$prefix.$child->nodeName][] = $this->node_2_array($child);    

            }

            else

            {

                $result[$prefix.$child->nodeName] = $this->node_2_array($child);

            }

        }

        

        if (!is_array($result))

        {

            $result['#text'] = html_entity_decode(htmlentities($dom_element->nodeValue, ENT_COMPAT, 'UTF-8'), ENT_COMPAT,'ISO-8859-15');

        }

        

        if ($dom_element->hasAttributes())

        {

            foreach ($dom_element->attributes as $attrib)

            {

                $prefix = ($attrib->prefix) ? $attrib->prefix.':' : '';

                $result["@".$prefix.$attrib->nodeName] = $attrib->nodeValue;

            }

        }

        

        return $result;    

    }

    

    /**

     * Generated XML Dom

     *

     */

    private function get_dom()

    {

        if(empty($this->xml))

        {

            echo 'No XML found. Please set XML data using setXML($xml)';

            return false;

        }

        

        $this->xml_dom = @DOMDocument::loadXML($this->xml);

        

        if($this->xml_dom)

        {

            return $this->xml_dom;

        }

        

        echo 'Invalid XML data'; exit;

    }

} 

$xml_data = file_get_contents('app.xml');

$converter = new Xml2Array();

$converter->setXml($xml_data);

$xml_array = $converter->get_array();

echo "<pre>";

print_r($xml_array); 





?>


this code its okay, but i cant get description texts

my description texts is:
DHCL
DH8
CD5
CLI8

please check and help

thank you




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users