Reading from an xml file


#1

Hi I’m trying to import an xml file into my app from a directory within my application code, to use as reference data, is there a ‘best practice’ way of doing this? My end goal is to pre populate a application specific database with the xml data.

Thanks in advance

John


#2

Use $http

https://docs.angularjs.org/api/ng/service/$http


#3

I dont think that you can read a file using the $http service, please correct me if I am wrong but I’m sure it’s only for consuming web end points?


#4

I may have found the solution:

http://cordova.apache.org/docs/en/2.5.0/cordova_file_file.md.html

I’ll post back here when I have given it a shot.


#5
  1. i think you can request all files of type ‘xml, json, script, or html’ via $http with a GET-Request and it will return the file-content
  2. you can load a file from your phone over the file-plugin FileReader… and so on.

Greetz, bengtler


#6

I searching for the same answer, i receive xml from a webservice and need to converto to JSON, can cordova file do this task ? or what is the best way ?


#7

There are some native javascript parsers.
http://goessner.net/download/prj/jsonxml/

and there is also an own jquery-plugin.

But keep in mind xml code can contain characters or structurs which would be invalid in json so the converting can fail!
So the best solution is to extend the API to return also JSON-Data if you want.

Nowadays nobody would build a xml-API for Web-/ Mobile-Apps.


#8

I found this class, try and it works: https://lostechies.com/seanbiefeld/2011/10/21/simple-xml-to-json-with-php/ so i will consume the xml webservice, generate the json output webservice.


#9

Yep simpleXML for php is a good choice.


#10

which is the best way to read data from app loicaly waiting for your reply


#11

two ways:

  1. The file is stored in your application folder --> you can simply make a get request
  2. The file is stored somewhere else accessible in your app --> use cordova file to mount the correct file system and access the file

But your question has nothing to do with the original topic right? :wink:


#12

Thanks for your reply,
Yes :grin: