Jump to content

Why XML?


Fmdpa

Recommended Posts

I understand that XML is a well structured way to store, share and show data. But frankly, I find that databases fulfill all of my needs in the area of data storage. As far as displaying information goes, RSS feeds are the only thing useful thing I've seen XML do. What are some practical uses for XML?

Link to comment
Share on other sites

Here's a thing I've gotten to like a lot. I store a bunch of data in an XML file. Clever use of element names and attributes creates all the relational ability you expect of a DB. My server does not interact with the file except to fetch it and save it, via AJAX.When my web app wants the data, AJAX fetches the complete file. The Broswer turns it into a DOM Document. DOM methods let me construct an editable table, form, whatever. The user edits it, and other DOM methods update the document. When the user submits the document, AJAX sends a serialized (string) version of the entire document back to the server.This is not a solution for every problem.What I like about it is that all the data is manipulated only in the browser. With a DB situation, you have to parse the data on the server. Server sends it to the browser, or embeds in a document. User makes changes. And now a stream of data returns to the server, where it must be parsed all over again before being saved to the DB. With a smallish XML file (<50K) there's a kind of elegance to skipping all that parsing, and also to keeping the entire data structure intact.Obviously you don't send down a file full of passwords and other vulnerable junk. But for the right app, it works pretty well.I'm sure boen will have some other views from an XSLT standpoint.

Link to comment
Share on other sites

Fmdpa, databases are not the answer to everything, and a lot of the time they're incredibly unnecessary. The only thing I can see a database really being necessary for is a session system or some kind of really advanced user system, and even then, unless your website is the next YouTube all you really need is SQLite. But then again about sessions, look at PHP sessions, they don't need a database at all.Not to mention, if you create a project and avoid using a database for something (something you probably don't need a database for anyway), your project becomes more portable. It doesn't have to rely on a server having a particular database system at all.Of course, the acts of using of XML to store data and using an RDBMS to store data aren't mutually exclusive. There are many situations where you may need to input very complex data into a particular field in a DB. You could use XML to store more complex data for a single field, however many people may prefer JSON over XML for a task like this.edit: Also, I'm confused, you mentioned RSS feeds? Are you asking for an example of how generic XML is useful for storing data, or for an example of how an XML-based language is useful?If it's the latter: http://en.wikipedia.org/wiki/List_of_XML_markup_languages

Link to comment
Share on other sites

unless your website is the next YouTube all you really need is SQLite.
Ok, I'm not ready to debate SQLite vs. MySQL...:)The biggest disadvantage I see to XML files is that they can be viewed by anyone. I guess I could store things like user's comments in an XML file, but if I'm using a db already anyway for a login system, why not keep the comments in the db, too? It's easy to work with, and secure.
edit: Also, I'm confused, you mentioned RSS feeds? Are you asking for an example of how generic XML is useful for storing data, or for an example of how an XML-based language is useful?
Both. I brought up RSS and the single useful thing I've seen so far in XML-based languages. I was/am asking for more examples, as well as discussion expounding upon XML. The XML-based languages listed on Wikipedia mainly had very technical uses. What common applications could I use it for, where it would excel above the other options?
Link to comment
Share on other sites

XML and XML-based languages have different meanings. XML is a structured data storage language, which can be used for storing any type of data. XML-based languages, like RSS, are languages people have come up with using XML syntax for some purpose - RSS for describing syndicated content, XHTML for marking up web pages, etc. You could use XML to store your comments, but you couldn't use RSS (well, it wouldn't be appropriate).One advantage XML has is that data is structured like a tree, while databases are all two-dimensional and rely on relations to describe data that doesn't fit in that structure. Also, as Dilated pointed out, XML is very light while databases comparatively have a lot of overhead.You can use permissions to prevent the viewing of XML files on a public webserver, or just place them outside the document root. Databases, and XML files, are only as secure as you make them.

Link to comment
Share on other sites

Ok, I'm not ready to debate SQLite vs. MySQL...:)The biggest disadvantage I see to XML files is that they can be viewed by anyone. I guess I could store things like user's comments in an XML file, but if I'm using a db already anyway for a login system, why not keep the comments in the db, too? It's easy to work with, and secure.
They can be viewed by anyone? I suppose if you haven't set any file permissions and leave the files in a public directory, they could be viewable by anyone. Also keep in mind, like I mentioned above, XML doesn't only have to exist in a file. You could put XML into a DB field to store more robust data.I can give you an example of one thing I've used generic XML for in web development. During the development of one of my websites, I created a way of storing and organizing static pages on the site using XML. Each page's metadata and content would be stored in an XML file. It would look something like this for each page's xml file:
<?xml version="1.0"?><page>   <title>Page Title</title>   <heading>Page Heading</heading>   <content><![CDATA[	  <p>Quamquam, si plane sic verterem Platonem aut Aristotelem, ut verterunt nostri poetae fabulas, male, credo, 	  mererer de meis civibus, si ad eorum cognitionem divina illa ingenia transferrem.</p>   ]]></content>   <links>	  <link uri="/">possible link for further relevant material</link>   </links></page>

This allowed me to store all static pages' data in one directory using XML. My PHP script would then, using SimpleXML, open a page's corresponding XML file when a page was accessed, grab all the data and put it into a template I had set up for the static pages.

Both. I brought up RSS and the single useful thing I've seen so far in XML-based languages. I was/am asking for more examples, as well as discussion expounding upon XML. The XML-based languages listed on Wikipedia mainly had very technical uses. What common applications could I use it for, where it would excel above the other options?
I suppose it depends on your definition of "common". I'm guessing you want examples of XML languages that have an impact on the web. One example I would love to give is XBL2 (http://www.w3.org/TR/xbl/). In years to come, every web developer is going to want/need to learn XBL2 and know the spec inside and out. In my opinion, it will soon be one of the most ground breaking additions to web development, easily. It's based on XBL which was developed by Mozilla, originally to augment behavior of widgets defined by their other XML-based language: XUL, which is used by Firefox for extension development. Of course there is SVG, which is currently the only real standardized means of delivering vector graphics to a browser. There is also MathML, which is currently being used frequently by Wikipedia to express mathematical equations in articles pertaining to math/physics/etc.Although I don't advocate it personally, I know many people would argue that XHTML is a useful XML-based language. I even think that HTML could be an XML-based language if XHTML were specially allowed some kind of graceful error handling by browsers. I even think HTML could have been an XML-based language, but unfortunately, HTML was invented long before XML. Currently though, XML has removed the need to invent an entirely new SGML-based language whenever we want a new markup language.
Link to comment
Share on other sites

XML is really a set of rules for defining languages (and rules for parsing and serializing them, but those are not details you need to worry about)... nothing more, nothing less.The primary usage XML usually finds is the mentioned structured data storage, with that usually implying a lightweight RDBMS replacement.Markup languages you use are essentially just that - XHTML stores structured data for a document, RSS stores structured data about stuff that's new, SVG stores structured data that describes a graphic, MathML stores structured data that represents equations (which are a kind of complex structures), etc.The point where you may find use for XML is when you find yourself in a situation thinking "Isn't there a simpler way to do this than [insert complicated PHP/JavaScript API]?". The answer in that case is usually "No, but it can be made simpler by creating a new XML language for that. Processors that get it will use [insert complicated PHP/JavaScript API] or another similar API in accordance with the XML description".Example: Isn't there a simpler way to create vector graphics than Flash? Something which can be easily edited and from which data could easily be extracted or which could easily be generated from data?... once upon a time, someone at W3C answered "No, but we can make a new XML language for that. Processors that get it will use DirectX, OpenGL, GDI or other graphic APIs to generate vector graphics. Someone may even generate a Flash equivalent from this new language"... and while they initially created VML, they later realized they need a lot more stuff, so they turned it into SVG, which we now use. And there is even Google's SVGWeb which does just the SVG-to-Flash convertion mentioned.

Link to comment
Share on other sites

That's a lot to think about! Thanks for bringing up file permissions. I hadn't thought of that. I'm glad you brought up SVG, XBL and XUL. I noticed that Opera extensions also use some form of XML also. (probably all browser extensions do). But I'm still not grasping very well how I could use XML itself. I'm beginning to see uses for XML-based languages, but not so much XML itself. I don't see why you would use it in databases because data is already split up between columns. Maybe my real problem is this: the way I cement something in my mind and understand it is by doing something with it, and I don't know what to do with XML.

Link to comment
Share on other sites

That's just it - it's the headline of the XML tutorial - XML doesn't "do" anything. It's just plain text that may be interpreted in different ways by different programs. You can define such programs yourself, but XML itself remains just plain text.It just so happens there are many different programs with each accepting various kinds of XML for various kinds of tasks. An XSLT file doesn't mean anything to an SVG viewer, and vice-versa. However, all SVG viewers and XSLT processors alike use DOM or a similar API to read those files, since they are, after all, XML files.You can in theory create an XSLT processor written in pure PHP (or any programming language) just by the use of DOM... in practice you don't need to of course, since there's such a processor built into PHP that will be faster... but imagine if there was no such thing. Imagine if you had another language for which PHP had no such support and which you needed - you can wirte an interpreter for that language in PHP or, if you're brave enough, in C (and just make it a PHP extension). Now imagine there actually isn't any XML based language that lets a processor do the thing you want to do - there are just different APIs in different languages for the same thing.Wouldn't it be nice if you could create two or more engines (one for each language/API), and have one kind of a description file that would work with both? You can do that, and your format may be either a binary one (like PDF, WAV, BMP, etc.) or a textural one. XML, similarly to JSON and CSV is a kind of textural file... a kind of file which can be given to those engines... but unlike JSON and CSV, XML lets you do more complex structures (e.g. mixing your description format with another one by the use of namespaces), which you'll probably need if you're already in need of unifying APIs.Different XML based languages are just the manifestation of that scenario. They each serve a need that was previously doable with programming languages and/or graphic APIs, but was doable in different ways across languages and with different APIs... Let this last statement sink in.

Link to comment
Share on other sites

Maybe I'll create a small experimental website using XML files as the database. I might even take on the challenge of a writing a parser. I'll also think about learning SVG and how to build browser extensions. Many of the concepts common in modern web technologies, like APIs using XML, are difficult for me to grasp. I'm sure it will just take time to understand it.

Link to comment
Share on other sites

SimpleXML is simple. The DOM is much more flexible though.P.S. there are actually lots and lots of different ways of parsing XML in PHP, all with different purposes. A list can be found at http://www.php.net/manual/en/refs.xml.php.

Link to comment
Share on other sites

simpleXML vs. DOMDocument?
DOM is more powerful (i.e. has more features) and its complexity, while greather than that of SimpleXML, is a constant (i.e. doesn't vary depending on scenarios).SimpleXML is less powerful (i.e. has less features) and its complexity grows from being a lot simpler than DOM (for "simple" scenarios) to being (IMHO) more complex than DOM.The parser and serializer for XML are the two things you don't need to write... that's the whole idea of XML - use a single parser for a variety of languages, expose the results with a certain API (DOM or SimpleXML) but define a different "processor" to give semantics to each language.
Link to comment
Share on other sites

Just my two cents, and this has been mentioned, but XML can go further than databases in the details of data stored in a single area. Take a transaction system, for instance. In one XML file, you could have something like so:

<transaction id="254">	<customer-id>16</customer-id>	<date>		<day>21</day>		<month>12</month>		<year>2010</year>	</date>	<purchase>		<item>			<item-id>13</item-id>			<quantity>3</quantity>		</item>		<item>			<item-id>24</item-id>			<quantity>1</quantity>		</item>		<item>			<item-id>72</item-id>			<quantity>5</quantity>		</item>	</purchase>	<total>23.60</total></transaction>

Now, I'm pretty sure the XML is valid, but I just typed it up on the fly, so don't be too harsh.The point I'm getting at is that storing this in a database would require either A) A new table with a row for each item that has a column that specifies which transaction it goes to, which is turn would be in a different table, or B ) You would have to store this data in a single field somehow and parse it. Either way, you're going to have to do a little more work than if you had an XML file. If you wanted to do a database, you could use method B and store the data in the field as XML and save the need for a whole new table. This was already mentioned as well.The point is if you have a lot of detail, a database doesn't have the capability of storing it all in one table. You would have to make new tables for the deeper levels. An XML file can do as deep as you want it to.Another reason XML can be useful is if you want users to be able to save data on their computers. Not a lot of general users have databases and such installed on their systems. But, your users have browsers. All they would need to view this information later is the XML file and (probably) an XSLT file of sorts to make the information more readable. This could be a useful way for users to save receipts, and also give you a way of allowing the user to upload receipts to see the status of transactions, as a random and probably not so good example. The point is, you can share data between yourself and a user without assuming the user has anything more than a browser installed.Overall, XML gives your portability and allows for more detail without having to do much more work for it at all. If you're not looking to pass around the data you are storing, or if you don't particularly care about having to create multiple tables and doing joins to provide deeper details, then XML doesn't offer you much in data storage.There are many applications to XML, as well. I'm not too experienced in using XML widely, myself. I use it for some of my PHP, C/C++, and Java applications that need to store data (for things such as configurations, saving progress, etc.). Other than that, I haven't gone real deep into the more complex possibilities of XML.

Link to comment
Share on other sites

Basically, yeah. If you need more complex stuff, you end up either doing weird manouvers in SimpleXML or export it to DOM, do the complex stuff, and turn it back to SimpleXML when you're done with that part.

Link to comment
Share on other sites

I'll probably learn SimpleXML first, anyway. It probably won't be difficult to pick up.
For the record, I've honestly never had to deal with XML so complex that it has gotten me to use DOMDocument over SimpleXML. You will pick up SimpleXML very quickly, it's probably one of the most intuitive things there is.Just look at some of the examples and you should be good: http://us3.php.net/manual/en/simplexml.examples-basic.php
Link to comment
Share on other sites

Thanks for the recommendation, Dilated. It's funny how when you start looking for something (in this case, XML tutorials) it suddenly appears! Tutorialzine, a site that I follow, had never, in my memory, posted a tutorial involving XML. But just a couple days ago, they posted a tutorial on how to create a customer testimonials page using XML as the "database".

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...