0

test.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<propertyList>
  <business  type="food">
  <listingAgent id="1">
    <name>Spiro Abelas</name>
    <email>[email protected]</email>
  </listingAgent>
 </business>
 <business  type="food">
  <listingAgent id="2">
    <name>andy</name>
    <email>[email protected]</email>
  </listingAgent>
 </business>
</propertyList>

Php code Interprets an XML file into an object

<?php
  if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');
      echo "<pre>";
      print_r($xml);
      echo "</pre>";
  } else {
     exit('Failed to open test.xml.');
     }
 ?>

Output

     SimpleXMLElement Object
(
[business] => Array
(
[0] => SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => food
        )
    [listingAgent] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 1
                )
            [name] => spiro
            [email] => [email protected]
        )
)
[1] => SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => food
        )
    [listingAgent] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 2
                )
            [name] => andy
            [email] => [email protected]
        )
)))

Table structure

enter image description here

i want to inset data like that into table

enter image description here

but i not getting how to retrieve data from array and insert into multiple rows

so plz guide me how to sort out it

1

1 Answer 1

1
foreach($xml->business as $row)
{
  $business_type = $row->attributes()->type;
  $id = $row->listingAgent->attributes()->id;
  $name = $row->listingAgent->name;
  $email = $row->listingAgent->email;
  mysql_query('INSERT INTO table…');
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for reply, but i m getting those error Call to a member function attributes() on a non-object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.