Background:
So i'm writing a web-service style web application as a way to increase my knowledge of how PHP and XML work together. I want to eventually take that XML data and use it in a mobile phone application but that's a different issue. I can connect to the data, pull, and process all the information with PHP and I've managed to get it exporting to CSV. I want to now begin to push that data out in XML.
Question:
What is the (a) recommended way to work with XML in PHP?
References:
PHP Manual, XML Portion
I suggest using simple XML which is way easier to handle xml operations.
Related
I have developed a VBA spreadsheet that runs locally and with obtain data from other sources continuously.
I want to find a way to extract / access the data inside this spreadsheet through php and then output some processed data using a browser and these data will update accordingly as the spreadsheet updates itself.
Anyone can show me how to achieve this? Both the spreadsheet and the php will run locally on a windows machine if that is easier...
Thanks very much for your help :)
I just developed an application that does this the idea is to use http requests on_change events in the spreadsheet.
vba code for request in excel
Function httpRequest(ByVal path As String) As String
Set HttpReq = CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.Open "GET", path, False
HttpReq.Send
httpRequest = HttpReq.ResponseText
End Function
On your php side you have to develop the part where the data is mapped in the database. And of course the view of the data. As for realtime you have to go for something more elaborate like a table that holds the latest changes and check for such changes every now and then unless there is some mechanism to do so.
In excel there exist timers to acomplish polled checking for updates
for web you should to use ajax for seamless updates have a look at http://datatables.net/
You can use PHP Excel Reader to display the data quite easily from the spreadsheet - I think this is the simplest of the PHP Excel libraries that will do what you want and simply set the page to refresh on a regular basis.
Using this library is pretty much as simple as this:
<?php echo $data->dump(true,true); ?>
Once you have told it where the file is to display the output pretty much as would be expected to see in an Excel Workbook. (Here is a link to an example that they have up and running online)
This means you won't get any locking issues as the read is done rather quickly (assuming a reasonable file size and users won't really ever be more than a few minutes/seconds behind.
You want PHP to just generate HTML from Excel sheets? Excel supports HTML generation natively.
Try Save as Web Page to test the quality of HTML produced by Excel. If it's OK then all you'll have to do is write a small piece of VBA that will be generating HTML from your spreadsheets automatically.
Sort of CMS build in Excel.
I have 30 android clients that send xml files to the server every one hour.I need to read each file which is pretty long(over 1700 lines per file) but i am not so sure how reliable is for this kind of tasks.
Should i continue reading with php or is C++ or java program better suited.
With PHP you can use 2 good library: SimpleXML for simple functions or DOM for something more complexe.
You can have a idea of what PHP can do with XML files here: https://www.php.net/manual/en/refs.xml.php
If you know the names of nodes you need to read, SimpleXML is the simple way to manage yours datas. You actually can do what you want with XML files on PHP.
I'm using the National Weather Service's REST web service to return forecast data for locations. I can get the data back and display it onscreen by calling an XSLT, and I can use XSLT to transform the returned XML to a file of SQL insert statements that I can then import manually. However, I'm a bit confused as to how I can do this automatically.
Some clarification: I need to have a cron job run on a scheduled basis to pull in data from the web service. I then need to somehow parse that data out into database records. This all needs to happen automatically, via the single php file that I'm allowed to call in the cron job.
Can anyone give me an idea of how I'd go about this? Do I need to save the XML response to an actual file on my server, and then transform that file into a sql file, and then somehow (again automatically) run an import on the SQL file? Ideally, I wouldn't have to save anything; I'd just be able to do a direct insert via a database connection in my php file. Would that work if I looped through the response XML using a DOM parser rather than an XSLt file?
I'm open to any alternative; I've never done this before, have no idea of how to go about it, and have been unable to find any kind of articles or tutorials about parsing XML data into a database directly.
You need to parse the xml data instead of using xslt to transform it.
You can use xml_parse_into_struct to turn it into a php array and work from it there.
It is probably easier to use SimpleXml and walk the xml tree though.
Considering you already have an xslt transformation, you can also write out the sql to a file, and pipe it to mysql directly.
exec("echo xml_sql.txt| mysql -uusername -ppassword database")
Good Luck!
I'm writing a simple cms for a client and for the umpteenth time can't decide the best strategy going in.
My options, as far as I can tell, are:
1) Store my XML data as a flat file that is referenced by my actionscript. I could manipulate this file with a php "back end".
2) Have my actionscript call a PHP script that would output the XML, bypassing the interaction between actionscript and the physical xml doc. (eliminating some caching issues).
3) Create flat .txt files (similar to tables) that php reads from and outputs XML to my actionscript call.
I'm up in the air about this because I'm not big on XML being my physical data. One little missing ">" and your whole file takes a dump. At least with option 3, one file might be corrupt, but it wouldn't bring down all of your data. Then again, I haven't made much use of PHP's built in XML classes, so could be just adding extra work to my project.
Anyone have a rock solid method for storing data when you don't have access to a Database?
Thanks in advance,
-J
I would personally take the hit on the write and use a tool (e.g. CMS) to create the XML file. You can manage the validation, creation, and manipulation within your language of choice.
I have a large XML file (600mb+) and am developing a PHP application which needs to query this file.
My initial approach was to extract all the data from the file and insert it into a MySQL database - then query it that way. The only issue with this was that it was still slow, plus the XML data gets updated regularly - meaning I need to download, parse and insert data from the XML file into the database everytime the XML file is updated.
Is it actually possible to query a 600mb file? (for example, searching for records where TITLE="something here"?) Is it possible to get it to do this in a reasonable amount of time?
Ideally would like to do this in PHP, though I could also use JavaScript too.
Any help and suggestions appreciated :)
Constructing an XML DOM for a 600+ Mb document is definitely a way to fail. What you need is SAX-based API. SAX, though, does not usually allow XPath to be used, but you can emulate it with imperative code.
As for the file being updated, is it possible to retrieve only differences anyhow? That would massively speed up subsequent processing.