Forum Moderators: open

Message Too Old, No Replies

Why XML?

Over MySQL and DBs, why should I use XML?

         

ahmedtheking

1:58 pm on Mar 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A lot of my friends and collegues have been telling me to learn XML, so I did! I picked up a book and read it! But to me, all I could gather from it was that XML is another form for a data storage solution, except, XML is like a flat file DB!

So, why should I use XML? What adv does it give me, and where will I use it? A lot of people have told me to use it with security applications (expecially when tied in with PHP, because of a few security flaws), but how?

At the moment, I haven't found a convincing enough opinion to change to XML! So i get to use my own tags, Ive spent time mastering XHTML, SQL and other languages, and now I have to make one up myself? Why?

If the application I make works, why do I need XML?

Please help! :D

mattur

3:03 pm on Mar 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



XML is not a replacement for databases.

XML is a human-readable, unicode text-based, data interchange format - and it can be used for storing data (in the same way any other text format can).

The keyword here is format. Perhaps an appropriate comparison question here is: "so why should I use Comma Separated Value (CSV) text files? If the app works why do I need CSV?"

Rather than looking for a way to use a particular format, you should look at what you want to actually do - applications.

Want to syndicate headlines? Use XML-based formats RSS or Atom.

Want to provide a Froogle feed? Use a CSV file.

The advantages of XML are its adaptability, its readability, and its wide range of processing tools - that can be used on any XML file regardless of the specific application - but if you don't need it, don't use it!

I've no idea what XML has to do with security(?).

brucec

3:05 pm on Mar 5, 2005 (gmt 0)

10+ Year Member



It's funny, because I was wondering the same exact thing in the beginning. I think most people have the same question as you.

But after I started using XML more and more, the answer became clear to me. I am going to answer your question with a very obvious answer: Because XML is becoming popular as a data exchange format.

That answer may sound too obvious, but consider that while you code your XML and you want to share your data with another web site, all they need to do is use SAX or the DOM and they will be able to read your data.

You cannot do that with HTML and you certainly do not want people accessing your SQL databases. XML solves this issue as well. Since HTML is not well-formed and has no device to make it valid, you cannot use it to reliably share data with other people or web sites.

Also, consider reading in a comma-delimited, or tab delimited text file. You cannot know what the headers are for once they scroll off the page. With XML, you will know exactly your data is and how it's labeled.

And consider that MS Office 2003 and other non-scripting programs are supporting it.

My advice to you is to keep using it and eventually, you will see that it has its place over SQL and HTML.

if you are a really good programmer, learn the DOM and how to intergrate it with scripting languages to read your XML in like ASP, PHP, .NET, and Java. All these languages support it.

I hope this shed some light.

Bruce

brucec

3:09 pm on Mar 5, 2005 (gmt 0)

10+ Year Member



Like mattur said, XML is not a placement for HTML. XHTML is the replacement for HTML. XML is different than both of them with things in common, like XML and XHTML are both well-formed. XML and HTML have two different purposes and XML will NEVER replace HTML. XHTML is just the approach to make HTML well-formed.

ahmedtheking

3:24 pm on Mar 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks brucec! I heard about the whole SQL > XML > another person's site thing.

I understand how XML can be used with DOM, but only in a basic sense!

I read the Visual Quickstart Guide on XML and that's what confused me!

I think i need to set myself an XML project! What could i use XML for? (apart from RSS and Atom news stuffs (I kinda know how to do that..!))

Any good tutourials or books?

brucec

4:24 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



ahmedtheking, you said the DOM can only be used with XML in a basic sense. Don't believe that. The DOM allows you to access every node of your XML tree right down to the attributes, CDATA, and processing instructions.

SAX is good to if you have large XML docs, because SAX does not take up memory like DOM does.

Both SAX and DOM are good and they have their pros and cons, but the DOM gives you complete access to all your XML nodes.

You asked for some uses of XML. All I can tell you is that you just have to be creative and understand that XML has more uses than you think.

My uses of XML so far have been:
1. I trained a secretary on XML to create bulk emails in MS Outlook. I used XSLT to create the actual HTML.

2. One of my clients actually uses it to edit his navigation bar on his web site...One less maintenance task for me to worry about and him to bug me with. I use DOM and ASP to create the hyperlinks on the toolbar:

<?xml version="1.0">
<toolbar>
<link>
<text>Home</text>
<url>index.html</url>
</link>
<link>
<text>About Us</text>
<url>about.html</url>
</link>
</toolbar>

3. A substitute for all of my comma delimited files. Consider that it replaced the FileScriptingObject in my ASP apps.

4. A calendar of events on a Chamber of Commerce home page.

Bruce

ahmedtheking

4:37 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But the thing is, I can do all of these with PHP and MySQL.

What advantages would XML give me over these?

brucec

5:04 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



I also like PHP and MySQL. They work well together. However, what if you want to give somebody access to your data, but not your database? You could program your PHP to query the MySQL database and display it as XML. Then, anybody can use your data without screwing up the db.

ahmedtheking

5:29 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are there any tutorials on that?

ahmedtheking

5:29 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And any on Live Bookmarking?

brucec

6:01 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



I don't know of any tutorials, but you can just use the &lt; for < and &gt; for > and display the results of your MySQL in these tags. That's the way I do it.

ahmedtheking

7:20 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I think I get it! Thanks!

Hester

10:39 am on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The thing about XML is that it uses 'obvious' tags to describe the data. So someone new looking at it has a good idea what it is about. Example:

<day>10</day>
<month>03</month>
<year>05</year>
<surname>John</surname>
<firstname>Elton</firstname>
<middlename>Hercules</middlename>

And so on. Now compare this to a flat file database:

10¦03¦05¦John¦Elton¦Hercules

Is the first piece of data the day or the month? What about the third? And is "John" the first name or the last?

Here you can also see the downside of XML. It's bloated. All those tags (which are actually longer than the data inside them!). Whereas a flat file database can be made using only one character to separate (delimit) the data. It could be a comma, or a pipe.

Of course XML has benefits, otherwise there would be no point to using it. Besides human-readability, one is that it is easy to sort the data using XSLT. So you can output only what you want.

One of my clients actually uses it to edit his navigation bar on his web site... I use DOM and ASP to create the hyperlinks on the toolbar.

Why not just use raw HTML? Then you can simply include it via PHP or ASP. I do this on my site for the list of the latest comments.

XML is not a replacement for HTML. XHTML is the replacement for HTML. XML is different than both of them with things in common, like XML and XHTML are both well-formed.

Except that XHTML is XML. It can be given the XML declaration at the top and can be treated as well-formed (as you say). So an XML parser can (in theory) parse an XHTML document. (An HTML page is unlikely to be parsed successfully as HTML allows for errors, unless the webmaster is coding it precisely.)

ahmedtheking

11:02 am on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, so say that I have a table within my MySQL DB that is dedicated to news.

It goes like:

id, newsitem (the link), title, date, category, body.

How could I use XML to create an RSS feed or Live Bookmarking?

How would the data go from MySQL to XML?

Hester

12:05 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way would be to use PHP (or ASP etc) to read the data from MySQL (not sure exactly how) and then just output it as text to a file. (Ie: add XML tags around it.) Think of it not as XML but just a load of variables and strings.

Crude example:

Assuming you've pulled these values from your MySQL database...

id, newsitem, title, date, category, body

...and created variables with the same names in PHP...


$id
$newsitem
$title
$date
$category
$body

...then just build up the file something like this, before writing it with an .xml extension. I'm using a string $xmltext to define the whole text for an RSS feed, just adding to it bit by bit.

$xmltext = '<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>My RSS Feed</title>
<link>http://www.my example site.com</link>
<description>An example for Webmaster World</description>
<language>en</language>
<copyright>Copyright 2005 You</copyright>
<managingEditor>youremailaddress@here.com</managingEditor>
<webMaster>and@again.here.com</webMaster>
<pubDate>Thu, 10 Mar 2005 11:37:00 GMT</pubDate>
<lastBuildDate>Wed, 02 Mar 2005 21:24:00 GMT</lastBuildDate>
<category>News</category>
<generator>Notepad2</generator>
<docs>http://backend.userland.com/rss</docs>

<item>
<title>'.$title.'</title>
<description>'.$newsitem.'</description>
<link>http://www.your site here.com/posts/'.$id.'</link>
<dc:date>'.$date.'</dc:date>
<category>'.$category.'</category>
</item>

</channel>
</rss>';

And so on. (I'm not sure what some of your variables are, such as body, so I've left them out. The RSS tags are ones I use, so you might want different ones instead.)

If you need the PHP to write the file, let me know.

ahmedtheking

12:19 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, yea that's a good idea.

Ok, so I can do that, get PHP to write me an XML file for all my news, but then how is that news up to date? Will the XML file be updated everytime a new item is added or will the PHP script have to be run every time a new item is added?

So once I have the XML, where do I go from there to make my RSS newsfeed/Live Bookmark?

Hester

12:36 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah wait, my code should have been like this:

$xmltext = the top bit (defining the feed)

then a loop to grab all the data into variables

then spit out the XML tags, but not echo them, just add them to the string $xmltext so it keeps getting longer.

lastly, add the closing rss and channel tags.

Ok, so I can do that, get PHP to write me an XML file for all my news, but then how is that news up to date? Will the XML file be updated everytime a new item is added or will the PHP script have to be run every time a new item is added?

The PHP will have to be run every time.

So once I have the XML, where do I go from there to make my RSS newsfeed/Live Bookmark?

Simply make a link to the xml file PHP creates, on your website. Users can then subscribe to it like any other feed. (All feeds are just XML files. It doesn't really matter how you create them. I edit mine by hand in Notepad!)

ahmedtheking

9:46 pm on Mar 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok seems cool, I'll give that a go and get back to you guys (and gals!).

Thanks v much!

py9jmas

10:21 pm on Mar 13, 2005 (gmt 0)

10+ Year Member



I'm using a string $xmltext to define the whole text for an RSS feed, just adding to it bit by bit.

Which is all well and good until one of the variables you're inserting breaks XML's well-formedness rules. It's much better to use one of the many XML libraries that look after well-formedness for you. Otherwise you might find yourself serving broken XML every now and again. A bit like Webmaster World.
[webmasterworld.com...]

Hester

10:31 am on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good point. In a data-critical environment, I guess you should really be using the built-in XML parser in PHP. I've also used that myself, but because it was a ready-made script, I found it hard to tweak.

If you can guarantee the variables will always be the same (and not likely to include HTML which would require CDATA tags around it, or anything else that would invalidate the XML) then my approach may be suitable.

Of course if a variable is blank, you will need to echo an empty string, rather than two tags with nothing between them. Eg:

Not this:


<middlename></middlename>

But this:

<middlename/>

Or even leave the tag out altogether, if valid to do so.

danmccarthy

4:30 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



The version of phpMyAdmin that my host offers has the ability to export a database table as XML. This is definitely easier in the short term, although a script might be easier to deal with in the long-term as far as updating that XML file.

ahmedtheking

4:48 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow, what version is it?

ahmedtheking

4:50 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PS, can XML be (maybe in the future?) dynamic?

For example, instead of having to make seperate XML documents for different news items, is it possible to have on XML 'template' (much like a PHP doc) that has data added to it on the fly?

danmccarthy

5:04 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



The documentation says it is "phpMyAdmin 2.6.0-pl3", but my web host customizes most add-ons to their servers, so I can't say for sure that I'm using the standard downloadable version of phpMyAdmin.

ahmedtheking

4:39 pm on Mar 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What about XML with flash?

twist

6:17 am on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I downloaded a recent version of phpMyAdmin a few months ago and it comes with the option to export into an xml file. Although its more than twice the size of a SQL export.

Don't quote me on this but I think I tried re-importing the xhtml file back in to see if it would work and it wouldn't take it, but I could be wrong or have just done it wrong, so just try it for yourself.

brucec

6:05 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



My PhpMyAdmin also exports to XML, but I don't like the tags that they use.

I do it like Hester suggested by using PHP or ASP and connect to my database, be it SQL or MySQL and create my own XML tags. This way, I am always in control of the XML and not the XML elements created by PhpMyAdmin.

Also, if you guys like PHP and want to process XML, consider using the SAX API for PHP. It is really good for reading in XML.

Bruce

mrMister

8:14 am on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



XML will NEVER replace HTML

You realise that XHTML can be XML right?

ahmedtheking

10:45 am on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I belive that XML will never replace HTML because it'll never be HTML! In HTML you have spec tags like font, and b, and h1 and so on, how will that be mimiced in XML? Via CSS? So everyone has to learn CSS before the make a website? Why? What's the point of Johnny learning CSS and XML to make a site, when he could prob make a better site just by HTML spec tags!

Personally, I think XML is a waste of time! I've only seen point in it for RSS, Flash <> PHP (but it can be done without, easier and faster without XML!).

I think we're taking a HUGE step backwards, because XML is not dynamic! Why have something like XML and not make it dynamic?! It's not secure. Say you have your database in XML format, anyone could look it up! It's pointless, why have an XML 'flatfile database' and lose all the advantages of having a structured and relation database?!

The world has gone insane!

mrMister

4:53 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<quote>XML is not dynamic!</quote>

Nor is HTML, that doesn't stop people creating dynamic web sites with it!

This 45 message thread spans 2 pages: 45