Forum Moderators: open
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
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(?).
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
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?
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
<day>10</day>
<month>03</month>
<year>05</year>
<surname>John</surname>
<firstname>Elton</firstname>
<middlename>Hercules</middlename>
10¦03¦05¦John¦Elton¦Hercules
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.)
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
$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>';
If you need the PHP to write the file, let me know.
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?
$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!)
I'm using a string $xmltext to define the whole text for an RSS feed, just adding to it bit by bit.
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>
<middlename/>
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.
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
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!