2

I have an XML doc used as database. It includes many <product> elements, each with children like <price>, <name>, <quantity>... Example:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <product code="AS542EG2542">
        <title>Best product in the market</title>
        <category>something 1</category>
        <price>10.99</price>
    </product>
    <product code="AS542EG2542">
        <title>Second best product in the market</title>
        <category>something 2</category>
        <price>58.99</price>
    </product>
    <product code="AS542EG2542">
        <title>Third best product in the market</title>
        <category>something 2</category>
        <price>24.99</price>
    </product>
</catalog>

Then, in a website, I'm using AJAX to call for it, and then retrieve information and insert it in the DOM:

// Get the XML document
var request = $.ajax({
    url: "http://www.example.com/resources/database.xml",
    dataType: 'xml',
    cache: 'false',
    method: 'GET',
    async: 'true'
});

// After AJAX has been completed, run the following function
request.done(function() {
    testingFunction(request);
});

// The function that extracts data from XML and inserts it into the DOM
function testingFunction(xml) {
    var xmlDoc = xml.responseXML,
        xml = $( xmlDoc ),
        // For each <product> elementt there is in the XML:
        productsList = xml.find("product").each(function () {
            // get child <title> of each <product>
            var titleValue = $( this ).find("title").text();
            // get child <price> of each <product>
            var priceValue = $( this ).find("price").text();
            // create a <li><div> titleValue </div><div> priceValue </div></li>
            $("ul").append('<li><div class="iss-title">' + titleValue + '</div><div class="iss-price">' + priceValue + '</div</li>');
        });
}

(I'm relatively new to programming)

Is this good practice? I've read XML is for organized data, not for storing them as database. Also I've read about SQL databases provided by e.g. MySQL, PostgreSQL, etc and that I can get data from their database tables using PHP to inject it into the DOM.

Would in my case be better to use a SQL database like MySQL? Following these criteria:

  1. Is good practice. E.g. as I said XML is for data used to be transported in a readable way for programs.
  2. Google bot can read/crawl the dynamically generated content from the SQL database. I've read about XML and it looks like Google won't crawl AJAX.
4
  • Why do you pass request to testingFunction at .done() callback? googlebot crawls everything it can. Commented Dec 28, 2016 at 22:13
  • 2
    I think you are looking for a review. So codereview.stackexchange.com is the proper place to ask this type of questions. Commented Dec 28, 2016 at 22:14
  • @DanCostinel Well, essentially yes. I didn't even know that one existed --there're lots of stackexchange pages! Commented Dec 28, 2016 at 22:16
  • Just look at the bottom of this page. Commented Dec 28, 2016 at 22:24

1 Answer 1

1

Would in my case be better to use a SQL database like MySQL? Following these criteria: Is good practice. E.g. as I said XML is for data used to be transported in a readable way for programs. Google bot can read/crawl the dynamically generated content from the SQL database. I've read about XML and it looks like Google won't crawl AJAX.

XML is going to be great for smaller data sets where there is not much data manipulation going on. Databases really need to be used when there is a larger data set and/or more data manipulation going on. What constitutes a small versus large data set? Depends on the program and the hardware on the device. To think about it, if you are using XML, to access your data a program will most likely have to start at the beginning of the file and read until the end, or until it finds what it is looking for. Using this thought process, and looking at your data, you should be able to tell if doing this will be processing intensive.
On the other hand, a database can actually manipulate data around, store it in various formats for better efficiency and communicate with various other platforms which it might not have first been intended.
I have been programming PHP and MySQL for over a decade, and it works pretty well. The main reason I am using a database in most cases is due to concurrent users. If you will have many different users, your XML file could be locked down as it is being accessed by someone, and another user will not be able to use it simultaneously. Databases, even MS Access, allow for concurrent users through locking down data at the row level, which means unless two people are using the precise same row, there won't be a conflict of users. So, looking at the size of your data, how much manipulation of data will be done and how many users will be using this concurrently should answer which approach is better...flat XML file or database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.