CodeIgniter Forums

Full Version: Parsing XML File Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ahmadalyan[/eluser]
hi all
I have XML file

Code:
<?xml version="1.0" encoding="utf-8" ?>
     <Resource>
            <item name="projectTerm">Project Term</item>
            <item name="projecttermText">Project Term Text</item>
            <item name="Langauage">Langauge</item>
            <item name="createnewTerm">+  Create New Term</item>
            <item name="projectTermForm">Project Term Form</item>
     </Resource>

i want to read this like

Code:
$project_term= $date['projectTerm'];

any one can help

El Forum

[eluser]pickupman[/eluser]
You should be able to read the string using [url="http://us2.php.net/manual/en/function.simplexml-load-string.php"]simplexml_load_string[/url].

Each node will be part of the object as you had shown instead of an array.
Code:
$xml = '&lt;?xml version="1.0" encoding="utf-8" ?&gt;
     <Resource>
            <item name="projectTerm">Project Term</item>
            <item name="projecttermText">Project Term Text</item>
            <item name="Langauage">Langauge</item>
            <item name="createnewTerm">+  Create New Term</item>
            <item name="projectTermForm">Project Term Form</item>
     </Resource>';
$data = simplexml_load_string($xml);
$project_term = $data->projectTerm;

El Forum

[eluser]ahmadalyan[/eluser]
Dear pickupman

it's doesn't work

when i try your solution and

Code:
$xml = '&lt;?xml version="1.0" encoding="utf-8" ?&gt;
     <Resource>
      <item name="projectTerm">Project Term</item>
      <item name="projecttermText">Project Term Text</item>
      <item name="Langauage">Langauge</item>
      <item name="createnewTerm">+  Create New Term</item>
      <item name="projectTermForm">Project Term Form</item>
     </Resource>';
  $XMLdata = simplexml_load_string($xml);
  print_r($XMLdata);

i had this output

Code:
( [item] => Array ( [0] => Project Term [1] => Project Term Text [2] => Langauge
[3] => + Create New Term [4] => Project Term Form ) )

so i can reach the item using
Code:
$XMLdata->item[0]; // the output will be Project Term

but i don't want to use the index 0,1,2 ... i want to use the name attribute to catch the key

El Forum

[eluser]ahmadalyan[/eluser]
i solved the problem

Code:
$this->lang->load('welcome');
   $xml = '&lt;?xml version="1.0" encoding="utf-8" ?&gt;
     <Resource>
      <item name="projectTerm">Project Term</item>
      <item name="projecttermText">Project Term Text</item>
      <item name="Langauage">Langauge</item>
      <item name="createnewTerm">+  Create New Term</item>
      <item name="projectTermForm">Project Term Form</item>
     </Resource>';
  $XMLdata = simplexml_load_string($xml);
  foreach($XMLdata->item as $node)
  {
   $attr = (string)$node->attributes()->name;
   $data[$attr] = (string) $node[0];
  }
  print_r($data);