whats the best way to process an xml to MySql in php - php

hi guys been struggling with this for a while, i can do xsl transformation on the data and that works to output the data, but i want to save the data i have tried automatically generating an array like shown here complex multidimentional associative array process with foreach but this didn't work as there are multiple accounts of room and i can only have the name ['room'] once in an array
so i need to know the best way to get xml to MySql using php

an easy way is to use PHP's integrated SimpleXML

Related

Parse XML data to JSON

In our environment we use MS SQL w/ stored procedures. In those procedures we return our results as XML and when access the data as we need to.
I'm introducing some charts into our tools which are 3rd party and require specific formats in order to operate and I am running into an issue.
In this screenshot, you are able to see what the structure should look like which I can get to work with the plugin just fine. The issue is with how SimpleXML handles single result sets.
As you can see in the image below, with one result item, it is no longer formatted as an array. The problem being is that the plugin expects to find the data in the format in the first example, but when there is only one value, it doesn't store it as an array.
As you can see from this image, the dataset for the escalationTypes is in the array format where the one below, submittedByDepartment is not.
I am trying to find out if there is something I can do to fix the root of this problem, with SimpleXML. Is this a common issue found with SimpleXML with a workaround?
UPDATE
Here is a sample of the XML Object I am working with: http://pastebin.com/uPh0m3qX
I'm not 100% clear what your required structure is, but in general, it's not a good idea to jump straight from XML to JSON or vice versa. Both can represent data in various ways, and what you really want is to extract data from one and turn it into the other.
Note that this is what SimpleXML is designed to help you with - it never contains any arrays, but it gives you an API which helps you extract the data you need.
If I understand correctly, you want to build an array from each of the dataset elements, and put those into your JSON, so you'd want something like this:
foreach ( $xml->children() as $item_name => $item ) {
foreach ( $item->dataset as $dataset ) {
$json[$item_name]['dataset'] = (array)$dataset->attributes();
}
}
Note that neither of those loops will behave differently if there is only one item to loop over. SimpleXML decides whether to behave like an array or an object based on how you use it, not based on what the XML looks like.
Note that while you can build a more general XML to array (or XML to JSON) function, getting it to always give the desired output from any input will probably take more time, and lead to harder-to-debug code, than writing specific code like the above.
you can declare the object as an array by adding (array) before referencing the variable. (array)$wasobject

PHP Array for specified JSON output

I just started working with JSON, so this is a hard way for me. I spent more than 15 hours trying to configure out how I should create and fill an array in PHP to get specified JSON format.
I need to get exactly this example1
{"players":["xZero","Lux"],"points":[{"player":"xZero","points":"20"}]}
and this
example2
{"players":["xZero","Lux"],"points":[]}
PHP will use foreach loops to fill data in example1 from database.
foreach($userdb as $users){
$output["users"][$i]=$users;
$i++;
}
This is just first half, I do not have any idea how to insert data in another half.
To get exact output like example1.
For example 2 I also don't have idea how to fill it out. I known for first half with users, but another half is mistery, like in example1.
As you can see, my experience with JSON is extremely poor. Also I'm not verry experienced with multidimensional arrays.
Thanks in advance for any help!
Based on your JSON your PHP array should look something like this:
array("players"=>array("xZero","Lux"),"points"=>array(array("player"=>"xZero","points"=>20)))
My best advice for you is to populate an array like this or create objects using classes and then using the json_encode() function.

Rails multi-array format in mysql

Have an old project that someone did in PHP and am attempting to convert it over to Rails. Am encountering an issue when trying to iterate through a multiple array in rails.
The array as stored in the DB looks like this:
a:5:{i:0;s:8:"Director";i:1;s:11:"Shareholder";i:2;s:14:"Vice President";i:3;s:9:"Secretary";i:4;s:9:"Treasurer";}
Am trying to display the String values such as "Director" and "Shareholder".
Does the DB field need to be changed to a different format to work?
How would this be done in rails?
Thank you in advance
That looks like the output of PHP's serialize function to me.
If you have PHP around or don't mind installing it to help with the data migration, then I'd write a short PHP script to convert those columns to JSON using PHP's unserialize and json_encode functions: read a value, unserialize it, json_encode the result, write it back into the database (all in PHP).
After that, you should be able to use ActiveRecord's serialize with JSON as the storage format:
class Model < ActiveRecord::Base
serialize :whatever_it_is_called, JSON
end
You could also use YAML (the default for ActiveRecord's serialize) if you had YAML support in PHP-land.
If you don't want to touch PHP at all, then you'll have to write Ruby parser for PHP's serialize format (which seems to be well documented in the PHP docs) and hook that up to ActiveRecord's serialize.
You will of course be working with a copy of the real database for all this work.

Building a multidimensional JSON array through PHP/MySQL

I am trying to build what I think is a multidimensional array in json through php and a connection to a mysql database.
Here's the output I am looking for:
[{"region":"Americas"},
{"region":"West","division":"Americas","revenues":"100000","headcount":"6"},
{"region":"East","division":"Americas","revenues":"1500000","headcount":"8"},
{"region":"South","division":"Americas","revenues":"1000000","headcount":"7"},
{"office":"San Francisco","location":"50 Kearny Street", "branch":"West"},
{"office":"Los Angeles","location":"200 Sepulveda","branch":"West"},
{"office":"New York","location":"Penn Street","branch":"East"},
{"office":"Philadelphia","location":"155 Trent","branch":"East"},
{"office":"New Jersey","location":"420 Broadway","branch":"East"},
{"office":"Atlanta","location":"39000 Parker","branch":"South"}]
This gives me the feed into a graphing program that creates the proper visualization. My database returns all the values in the JSON above (region, division, revenues, etc.) EXCEPT for "branch" which I had to hand code myself. I can't sure "region" again (because of the graphing algorithm) so I had to rename it to "branch." Under this current configuration, the "Americas" is the top node, with the "regions" underneath, and then the "branches" under each region as appropriate.
So far, I've only been able to build a basic JSON array using PHP and the json_(encode) function ; to get the output above, I edited the file by hand to make sure it would render properly in my graph, which it does.
The question I am looking to answer is: what is the fastest and easiest way to build the above array on the fly? Through a loop in PHP, I would imagine, but I can't get my head around what that would look like.
I probably left out information you need to help, so this is just to get the ball rolling.
Thanks!
Look at this topic: Looping a multidimensional array in php
You can loop through the array using the foreach construct

Is there a way to query JSON in PHP?

I've used Google, Yahoo, AND Bing, but I can't find any good answers. I've seen jLinq, but I want to be able to query JSON in PHP in hopes of having a not an SQL database, but instead all data storage within the filesystem on my server. No, I don't care how bad it sounds.
Ideas nonetheless? I would think that there would be a PHP class on this.
-----EDIT-----
Guys, thanks for your answers so far, but I don't think that json_encode and json_decode are of much use. What I want to be able to do is encode/decode JSON, and be able to search it for specific keys with specific values. Albeit I have PROVEN to myself that I can do so, it's a lot of code for something that should be so simple. Anything else you have in mind?
You can use JsonQ package. This package can query over JSON data like Query Builder.
https://github.com/nahid/jsonq
You can call json_encode to convert your data to a string, and write this string to a file. Then when you want to use it, you read the entire file and call json_decode to convert it back to data. When you're done processing it, repeat the encode/write steps.
But if you have multiple processes doing this, they'll completely overwrite what each other is doing. So it's not a very good way to manage shared data.
You could try JSONPath it allows you to query json with xpath as you would xml.
Look into json_decode. This lets you take JSON as a string and parse it into an object in PHP. You could theoretically store the strings as files, and use file_get_contents to retrieve the string from the file.
You would have to write your own searching/indexing/updating/etc algorithms, but if you don't want to use a real database solution (since you said, No, I don't care how bad it sounds.), then I guess this would work.
To search for values in the object you get from json_decode, look into in_array and array_search

Categories