0

I have a local server , where i am receiving xml files. The xml files are received just fine but i have problem on how to pass my XML file into my database.

I retrieve the xml file simple as that :

$xml_post = file_get_contents('php://input');

The xml files i receive looks like this :

<city>
<id>1081</id>
<name>athens</name>
<country>Greece</country>
<info>etc etc etc</info>
</city>

I have manually created a database with phpmyadmin with the exact same nodes so i can save everything correctly.

Now i want to insert what i am getting in the database with mysql_query. Something like this :

mysql_query("INSERT INTO cities (id, city ,country , info)
  VALUES (4 , 'athens' , 'greece' , 'etc etc etc'");

But which are the variables that i have stored my xml to use them as values in the statement above? How can i parse the xml file that i have to store all the nodes in variables and then pass them correctly to the database?

3

2 Answers 2

2

Try using simplexml_load_string()

Example usage:

<?php
$xml = '<city>
<id>1081</id>
<name>athens</name>
<country>Greece</country>
<info>etc etc etc</info>
</city>
';

$xml = simplexml_load_string($xml);
//1081
echo $xml->id;
//athens
echo $xml->name;
?>

If you have multiple nodes within your XML each node will be an array of objects:

$xml = '
<citys>
    <city>
        <id>1081</id>
        <name>athens</name>
        <country>Greece</country>
        <info>etc etc etc</info>
    </city>
    <city>
        <id>1082</id>
        <name>somwhere else</name>
        <country>Spain</country>
        <info>etc etc etc</info>
    </city>

</citys>
';

$xml = simplexml_load_string($xml);
//print_r($xml);

foreach($xml->city as $val){
    echo $val->name;
}

//or
echo $xml->city[0]->name;
//athens
echo $xml->city[1]->name;
Sign up to request clarification or add additional context in comments.

4 Comments

i tried $xml = simplexml_load_string($xml_post); but then when i try to echo something , for example echo $xml ->id; i get an error : Notice: Trying to get property of non-object . how can i access the elements?
It returns an XML element object. php.net/manual/en/class.simplexmlelement.php
i am sorry i cant understand this manual..would it be easier if you could tell me how to access the elements?
ok but how do i put in into the database. i try : mysql_query("INSERT INTO cities (id, city ,country , info) VALUES ($xml ->id , $xml ->city , $xml ->country , $xml ->info"); but nothing is inserted on my db
1

You would need to parse the XML into data readable by PHP, and then use that data in your SQL insert.

PHP SimpleXML Tutorial might be a place to start.

7 Comments

Yes, with the proviso that if the files won't fit in memory, an XML stream reader would be better. But they are a fair bit more complicated to use!
I think a stream reader would be to complicated for the OP, because they often need callbacks and only report the start/end of tags. But, they handle poorly formed XML better. It all depends on where the XML is coming from.
I tried to parse the xml using $xml = simplexml_load_string($xml_post); but then when i try to echo something , for example echo $xml ->id; i get an error : Notice: Trying to get property of non-object . how can i access the elements?
@donparalias - what does print_r($xml); give you? That will show you the structure you are working with, and is a good debugging technique generally.
the thing is that in this script i am receive xml files from other scripts , so if i just run it i get the error cause there is no xml file.. but now i think i am parsing it ok. i just dont know how to put it in the database.
|

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.