sepoto Posted May 4, 2012 Share Posted May 4, 2012 I have some data in a flat file. These are the fields: nameaddressziplatlng A row of data might look something like: fishing hole #1, Catalina Island U.S., 90704, 33.3, 118.4 There are fifty rows of data. I am trying with PHP to encode this data into an XML file. The ultimate goal is that the data will be stored on the disk in XML format and when requested PHP will read the XML file and retrieve the data and put it into an array so I can encode the data in JSON and send it back off to JavaScript (AJAX). I'm very familiar with working with flat files and SQL however XML is a bit unfamiliar to me as of yet. How would this be accomplished? Good Evening W3Schools! Link to comment Share on other sites More sharing options...
birbal Posted May 4, 2012 Share Posted May 4, 2012 (edited) you can use file() which will get you each line as array. you can then explode() the line as array. after that you can use dom to manupulate xml using dom. dom works same like you may used in js.http://php.net/dom techinicaly you may avoid the xml part .the array you get from file also can be encoded as json to pass it js,if it matches your situation. Edited May 4, 2012 by birbal Link to comment Share on other sites More sharing options...
sepoto Posted May 4, 2012 Author Share Posted May 4, 2012 (edited) Not yet! Edited May 4, 2012 by sepoto Link to comment Share on other sites More sharing options...
sepoto Posted May 4, 2012 Author Share Posted May 4, 2012 (edited) It looks very complicated! Could I see some code that would pertain to my data? I am seeing how to load and save the file that is easy. What looks much harder is how would be the best way to encode my data. Edited May 4, 2012 by sepoto Link to comment Share on other sites More sharing options...
birbal Posted May 4, 2012 Share Posted May 4, 2012 http://php.net/filehttp://php.net/explode it is as simple as i wrote the steps. you first check those function to see how they works . i am sure you will get the idea if youu look into those links. if you still have problem or any specific question come back,we will help you. Link to comment Share on other sites More sharing options...
sepoto Posted May 4, 2012 Author Share Posted May 4, 2012 Thank you Birbal! Those are excellent resources! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 4, 2012 Share Posted May 4, 2012 If you're working with CSV data, you might want to use fgetcsv. You might also just want to leave it in CSV format, fgetcsv returns an array of values that you can use to build your JSON array. http://www.php.net/manual/en/function.fgetcsv.php Link to comment Share on other sites More sharing options...
sepoto Posted May 4, 2012 Author Share Posted May 4, 2012 (edited) A great suggestion. I have started to wonder myself why I should be bothering with the complexities of XML. Perhaps I will build a .CSV directly to JSON. XML would be more fun though. Edited May 4, 2012 by sepoto Link to comment Share on other sites More sharing options...
boen_robot Posted May 12, 2012 Share Posted May 12, 2012 I have started to wonder myself why I should be bothering with the complexities of XML.I have one reason for you - JavaScript. The DOM you use in PHP is the same as the one in JavaScript... well... the spec is the same at least. The point is that if you master DOM in one environment, you can reuse your skills in the other environment. You can't really do that with CSV. You can sort of do it with JSON, except that you'd still end up using DOM with JavaScript, as it's tightly integrated with browsers' JS engines. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now