Converting MYSQL table data directly to an XML in PHP [duplicate] - php

This question already has answers here:
How to generate XML file dynamically using PHP?
(8 answers)
Closed 8 years ago.
I want to now if there is some inbuilt function that will create an XML directly from MYSQL result-set after executing SELECT query ?

I just wrote this and then thought id search to see if anyone else had written it. It looks simpler than the accepted answers tutorials. My $results comes from $result = mysql_query($query,$link);
$xmlDom = new DOMDocument();
$xmlDom->appendChild($xmlDom->createElement('results'));
$xmlRoot = $xmlDom->documentElement;
while ( $row = mysql_fetch_row($result) )
{
$xmlRowElementNode = $xmlDom->createElement('row');
$i=0;
for($i=0;$i<mysql_num_fields($result);$i++)
{
$xmlRowElement = $xmlDom->createElement(mysql_field_name($result,$i));
$xmlText = $xmlDom->createTextNode($row[$i]);
$xmlRowElement->appendChild($xmlText);
$xmlRowElementNode->appendChild($xmlRowElement);
}
$xmlRoot->appendChild($xmlRowElementNode);
}
header('Content-type: text/xml');
echo $xmlDom->saveXML();
This will procude XML in the form of
<results>
<row1>
<fieldname1>value</fieldname1>
<fieldname2>value</fieldname2>
<fieldname3>value</fieldname3>
<fieldname4...>value</fieldname4...>
</row1>
<row2>
<fieldname1>value</fieldname1>
<fieldname2>value</fieldname2>
<fieldname3>value</fieldname3>
<fieldname4...>value</fieldname4...>
</row2>
<row3...>
<fieldname1>value</fieldname1>
<fieldname2>value</fieldname2>
<fieldname3>value</fieldname3>
<fieldname4...>value</fieldname4...>
</row3...>
</results>
For any SELECT query.

There is no inbuilt function that will create XML directly from MYSQL result-set after executing SELECT query.
You have to write code for this
Some nice tutorials are this..
http://www.codediesel.com/php/converting-mysql-queries-to-xml/
http://www.mightywebdeveloper.com/coding/mysql-to-xml-php/

Generally your MySQL result will be returned as an array or an object, which you can then convert to XML or another format. You can use SimpleXML, which has been explained here: How to convert array to SimpleXML

Related

Cannot convert mySQL database entries text '\n' to '<br>'in Angular? [duplicate]

This question already has answers here:
Preserve line breaks in mysqli fetch_assoc( ) PHP
(2 answers)
Closed 2 years ago.
I can read data to my angular app from the database but I am just given the static \n in my text instead of a new line. I am aware I am supposed to convert all \n occurrences to <br /> but I am failing to do this, even when using echo nl2br;
//extract from my php file
if($result = mysqli_query($con, $sql))
{
$animals = [];
$cr = 0;
while($row = mysqli_fetch_assoc($result))
{
$animals[$cr]['animal'] = $row['animal'];
$animals[$cr]['t1'] = $row['title1'];
$animals[$cr]['p1'] = $row['paragraph1'];
$cr++;
}
echo nl2br($animals);
echo json_encode($animals);
Below is my angular file
//extract from my animals.component.html file
<div class="container" *ngFor="let animal of this.animals">
{{animal.t1}}
{{animal.p1}}
{{animal.t2}}
</div>
However, my output on the webpage (coming from animal.t1) is just the same text as the database entry:
Animals \n are \n fun \n .
I have tried numerous different things and just cannot seem to convert the \n's to <br>. Has anyone got any advice on this?
nl2br takes a string not an array. If you select the columns in the query it is much easier. Just map the row array:
// SELECT animal, t1, p1 FROM table WHERE .....
while($row = mysqli_fetch_assoc($result))
{
$animals[] = array_map('nl2br', $row);
}
echo json_encode($animals);
If Angular is converting HTML to entities then you may want to look here Using HTML Entities within Angular strings
Use nl2br:
$animals[$cr]['t1'] = nl2br($row['title1']);

Scraping data from php file

I am trying to get the car positions (if I can the cars state (gas, how clean, etc) from this site: https://carsharing.mvg-mobil.de/?ref=separate.
As far as I can tell they get their data from this URL:
https://carsharing.mvg-mobil.de/json/stations.php
Now I am having trouble converting that into usable XML format. I tried bringing it into String form by using
JSON.stringify()
and go from there but that didn't seem to work. What im having trouble with are the { and quotation marks
since your question is tagged as php, here is a simple code-snippet that will get you a xml-string:
<?php
//get json-string
$cars_json = file_get_contents("https://carsharing.mvg-mobil.de/json/stations.php");
//convert json to array
$cars_array = json_decode($cars_json,true);
//creat xml-object and fill recursive
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($cars_array, array ($xml, 'addChild'));
//create xml-string that can be saved
$cars_xmlstring = $xml->asXML();
echo $cars_xmlstring
?>

PHP: get parent node where child = 'value' [duplicate]

This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 7 years ago.
I have this xml file:
<friends>
<friend>
<name>xxx</name>
<pays>France</pays>
</friend>
<friend>
<name>yyy</name>
<country>France</country>
</friend>
<friend>
<name>zzz</name>
<country>USA</country>
</friend>
</friends>
To get my data, I am using this php code:
$xml = simplexml_load_file('friends.xml');
$friendsXML = $xml->friend;
Which works fine, but returns all of the friends.
Now I want to retrieve only friends who are from France:
country = 'france'.
Can anyone help me doing that?
I'd use XPath for things like this. Try:
$res = $xml->xpath('friend[country = "france"]');
echo $res[0];

Extract data from XML with file_get_contents to php [duplicate]

This question already has answers here:
Simple XML - Dealing With Colons In Nodes
(4 answers)
Closed 8 years ago.
I'm sending a request to an API which sends a response in XML format.
The response looks like this:
<ns2:HotelInformationResponse hotelId="263933">
<customerSessionId>0ABAAA85-112B-0914-9322-CA866D907EF8</customerSessionId>
<HotelSummary order="0">
<hotelId>263933</hotelId>
<name>Nova Platinum Hotel</name>
<address1>562 Moo 10 Pratamnak Road, Nongprue</address1>
<address2>Banglamung</address2>
<city>Pattaya</city>
<postalCode>20260</postalCode>
<countryCode>TH</countryCode>
How can I get this data so I can print individual values like hotelId or name ?
I have tried like this:
$Geosearch = 'APICALLURLHERE';
$fileContents = file_get_contents($Geosearch);
$string_data = $fileContents;
$xml = simplexml_load_string($string_data);
$hotel_id = (string) $xml->hotelId;
$hotel_name = (string) $xml->name;
echo $hotel_id.' '.$hotel_name;
Also I have tried this:
$xml = simplexml_load_file("APICALLURLHERE");
echo $xml->hotelId;
echo $xml->name;
Acces to remote files
The fopen family of functions, including file_get_contents and simplexml_load_file) by default do no allow access to remote files. This is a very good thing and should stay that way.
If you want to enable remote files, you have to change your PHP configuration.
http://php.net/manual/en/features.remote-files.php
allow_url_fopen = 1
Errors in a XML file
If your file loading is working but you STILL do not get your XML working, you may have a broken XML file. You can debug these issues with this snippet:
$doc = simplexml_load_string($xmlstr);
if (!$doc) {
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo display_xml_error($error, $xml);
}
libxml_clear_errors();
}
http://php.net/manual/de/function.libxml-get-errors.php

PHP json decode with number tag [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access object properties with names like integers?
I tried to decode the json result from youtube data api by the following code:
$url="http://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=jsonc";
echo "$url".'<BR>';
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json);
$items=$json_output -> data;
$content = "{$items->content->1}";
echo $content.'<BR>';
Everything works fine but the last two lines. Could someone please help?
And Here is the json result:
{"apiVersion":"2.1","data":{"id":"9jDg3Dh28rE","uploaded":"2012-10-04T03:45:49.000Z",
........
"content":{"5":"http://www.youtube.com/v/9jDg3Dh28rE?version=3&f=videos&app=youtube_gdata",
"1":"rtsp://v5.cache8.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"6":"rtsp://v5.cache4.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":2403,......}}
You need to wrap the numeric property with {} to access it.
$content = $items->content->{1};
And you also don't need to use double quotes like below:
$thumbnail = "{$items->thumbnail->sqDefault}";
This should just be
$thumbnail = $items->thumbnail->sqDefault;

Categories