CodeIgniter Forums
Using xml files for a web project. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Using xml files for a web project. (/thread-72989.html)



Using xml files for a web project. - HarrysR - 03-07-2019

The main question here is the following,
"Is it safe - ok - faster - lighter for the website (call it as you wish) to use an XML from server file or url directly and not storing them in the db, in order to get website entries?"

E.g.:
Let's say we have a website with multiple users in it and some of them having some blog posts. Is it ok and the most important, safe to use an XML to parse the data?


P.S.: I couldn't find any related subjects about this so i thought that it would be a good discussion subject for some newbies (like me Tongue Tongue Big Grin )
Thanks!


RE: Using xml files for a web project. - jreklund - 03-07-2019

You can't really sort or search that data, so I would not store it that way.


RE: Using xml files for a web project. - ciadmin - 03-07-2019

I have used XML to store "website" data for 20+ years. I don't make it directly accessible, but instead have a controller/servlet which retrieves it on the server, from a not-public-facing location, and then I typically render it as HTML in some fashion. I started this practice back when servers weren't as capable, and running a databse engine alongside your web server was considered excessive resource consumption. I switched much of my work to RDBs 10 years ago, but am now switching back to XML & markdown for content, as I find it easier to edit.

I do have some apps which store XML documents as columns in an RDB, but the extra care, encoding & validation led me away from that practice. I even had a couple of apps which stored XHTML in an RDB column, and then carefully dished those for in-browser editing with redactor - not the safest practice.

As for lighter or faster, however you measure those, I was working at one point with a 20,000 "record" XML document, with no noticeable lag dishing pages based on processing that.


RE: Using xml files for a web project. - ciadmin - 03-07-2019

@jreklund ... XPath is your sorting & searching friend, as well as XSLT serverside! SimpleXML has xpath capabilities. I haven't played with XSLT inside PHP, so can't comment on how appropriate it is.


RE: Using xml files for a web project. - HarrysR - 03-07-2019

(03-07-2019, 03:53 PM)ciadmin Wrote: I have used XML to store "website" data for 20+ years. I don't make it directly accessible, but instead have a controller/servlet which retrieves it on the server, from a not-public-facing location, and then I typically render it as HTML in some fashion. I started this practice back when servers weren't as capable, and running a databse engine alongside your web server was considered excessive resource consumption. I switched much of my work to RDBs 10 years ago, but am now switching back to XML & markdown for content, as I find it easier to edit.

I do have some apps which store XML documents as columns in an RDB, but the extra care, encoding & validation led me away from that practice. I even had a couple of apps which stored XHTML in an RDB column, and then carefully dished those for in-browser editing with redactor - not the safest practice.

As for lighter or faster, however you measure those, I was working at one point with a 20,000 "record" XML document, with no noticeable lag dishing pages based on processing that.

So let's say we have an XML file with data like "Post image", "Post title", "Post description" etc.. etc.. 
I made a test a couple of days ago and the result was fine.
Here's the example controller:
Code:
public function xmlview(){
$data['title'] = 'XML File';
$file = base_url('assets/xml/test.xml');
$xml = simplexml_load_file($file);

foreach($xml->children() as $child) {
  $data['items'][] = array(
   'title' => $child->TITLE,
   'year' => (int)$child->YEAR
  );
}

$this->load->view('templates/header', $data);
$this->load->view('pages/xmlview', $data);
$this->load->view('templates/footer', $data);
}

Would you suggest something like that?


RE: Using xml files for a web project. - ciadmin - 03-07-2019

Almost ... if base_url('assets/...') is publically accessible, you are exposing your data, which is not good.
A better practice is to map your document root to your "public" folder, put index.php inside that, and to keep any XML data in a separate folder inside the project (or even elsewhere on your server).

Eg.
project
- application
- data
- XML ...
- public
- assets/images, css, etc
- index.php
- system


RE: Using xml files for a web project. - jreklund - 03-08-2019

(03-07-2019, 03:55 PM)ciadmin Wrote: @jreklund ... XPath is your sorting & searching friend, as well as XSLT serverside! SimpleXML has xpath capabilities. I haven't played with XSLT inside PHP, so can't comment on how appropriate it is.

So there are a order by functionality. Who knew! Personally I will stick to relation based database. But if it's static pages, made with markdown like you said. It could be a valid option for sure.


RE: Using xml files for a web project. - HarrysR - 03-08-2019

(03-07-2019, 05:37 PM)ciadmin Wrote: Almost ... if base_url('assets/...') is publically accessible, you are exposing your data, which is not good.
A better practice is to map your document root to your "public" folder, put index.php inside that, and to keep any XML data in a separate folder inside the project (or even elsewhere on your server).

Eg.
project
- application
- data
 - XML ...
- public
 - assets/images, css, etc
 - index.php
- system

The file is stored there as an example. The real file might be in a separate folder as you mentioned or parsed from a URL. 
The main purpose was to retrieve the data from the XML and pass them to a view file in order to test speed and efficiency.

BUT, i got an XML file with up to 7000+ entries and the page was kinda slow.. (actually REALLY slow!!) What i wanted to be clarified with this discussion is that if that approach, of loading an XML file from a source (file or URL) is good for a page that contains many users and some of them might have some item "feeds" (like posts, or products or something else that can be parsed from XML).

I hope i made myself clear.


RE: Using xml files for a web project. - ciadmin - 03-08-2019

I think the approach is viable.
It might be possible to improve performance by separating the XML into multiple documents (one per thread? one per user?), to reduce parsing time.
It might also be possible to consider a document database (eg mongodb), though that is not directly supported in CI - it is a candidate & not fully thought out feature for CI4.


RE: Using xml files for a web project. - HarrysR - 03-09-2019

(03-08-2019, 10:23 AM)ciadmin Wrote: I think the approach is viable.
It might be possible to improve performance by separating the XML into multiple documents (one per thread? one per user?), to reduce parsing time.
It might also be possible to consider a document database (eg mongodb), though that is not directly supported in CI - it is a candidate & not fully thought out feature for CI4.

Well, i will take a further look on mongoDB!

Thanks for the suggestions!