Lowongan Kerja

Galleri Foto Artis

Gallery Tattos

XML

As you're aware, if only from the cameo appearance in Chapter 2, "Introducing Ajax," XML stands for eXtensible Markup Language, but other than the purpose of padding resumés, you're probably not aware of why XML is used so much. Basically, there are three main reasons for the popularity of XML, not including the air of mystery surrounding anything with an X in it. (Don't believe me about the air of mystery? What about The X-Files and X-Men?)
Literally tons has been written about XMLwell, at least when hard copy is taken into account. As for electronic editions, I can't say because my notebook seems to weigh the same, regardless of the free space available. For this reason, I won't bore you with the history of XML and how it is the best thing since sliced bread, or how it cures baldness, because it would be either redundant or an outright lie. Anyone who has ever developed an application that uses XML knows that there is a good chance of pulling out one's own hair when attempting to explain XML to fellow developers who still haven't grasped the software equivalent of the concept of fire. However, I should at least hit the highlights and point out some of the more useful and obscure topics.
8.2.1. Well Formed
Alright, the concept that XML has to be well formed is not obscure, but it does fall well into the useful bucket. You'd be surprised at the number of times that I've had to explain the concept of "well formed" to a particular project leader with mainframe roots. Or, come to think of it, maybe you wouldn't. Let's just say that, like the Creature from the Black Lagoon, the XML challenged walk among us, and you don't even need to travel to the now-closed Marineland in Florida to find them. For this reason, it is time for XML 101.
An XML document is well formed when the follow conditions have been met:
All opening tags either have a closing tag or are self-closing.
All attributes have values.
All the values for the attribute are enclosed in either quotes or apostrophes. I should point out, however, that they need to be consistent. This means no mixing and matching; if a quotation mark is used on the left side of a value, a quotation mark must be used on the right side.
Beware of entities! Wow, that sounds spooky, doesn't it? Entities are special characters that need to be handled with respect because, without special handling, they can be mistaken as something other than content.
That was relatively easy, wasn't it? I recommend quoting it verbatim whenever it is necessary to explain the concept to a clueless project leader. But you need to remember to make your eyes big when saying "Beware of entities!" because they like that.
Alright, now that you're (hopefully) open to XML, the big question is, where does it come from? Well, that depends on both your web server and database environments; some have built-in methods for producing XML directly from the result of SQL SELECT statements or stored procedures. If your environment doesn't support that, there is always the possibility of "rolling" your own XML. Because XML is human readableessentially, textwith a little work, it is possible to create XML, even where XML isn't supported.
Take, for example, MySQL. Although both Oracle and SQL Server can produce XML directly from a stored procedure, a little more effort is required to produce XML from MySQL. First, a stored function is required to build the individual nodes by concatenating the node name and value, as in Listing 8-1. Next, a function is needed that uses a cursor to step through the results of a query and build the XML using the aforementioned stored function. Listing 8-2 contains a sample stored procedure to do just that.

Listing 8-1. Concatenating a Stored Function
DELIMITER $$

DROP FUNCTION IF EXISTS `ajax`.`f_xmlNode`$$
CREATE FUNCTION `ajax`.`f_xmlNode`(
/*
To produce the text representation of an XML node.
*/
nodeName VARCHAR(255), /* XML node name */
nodeValue LONGTEXT, /* XML node value */
escape BOOLEAN /* Apply XML entity escaping */
) RETURNS longtext
BEGIN
DECLARE xml LONGTEXT; /* XML text node/value combination */

IF nodeValue IS NULL OR LENGTH(nodeValue) = 0 THEN
SET xml = CONCAT('<',nodeName,' />');
ELSE
IF escape THEN
SET xml =
CONCAT('<',nodeName,'>',REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(nodeValue,
'&','&'),'>','>'),'<','<'),'''','''),'"','"'),'eName,'>');
ELSE
SET xml = CONCAT('<',nodeName,'>',nodeValue,'');
END IF;
END IF;

RETURN xml;
END$$

DELIMITER ;
Listing 8-2. XML Producing a Stored Procedure
DELIMITER $$

DROP PROCEDURE IF EXISTS `ajax`.`itemSelectXML`$$
CREATE PROCEDURE `ajax`.`itemSelectXML`(
guildItemId INTEGER,
guildId INTEGER
)BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE xml LONGTEXT DEFAULT '';
DECLARE cGuildItemId INTEGER(6);
DECLARE cGuildId INTEGER(6);
DECLARE cGuildName VARCHAR(255);
DECLARE cItemName VARCHAR(255);
DECLARE cItemDescription VARCHAR(255);
DECLARE cItemPrice DECIMAL(10,2);
DECLARE itemCursor CURSOR FOR SELECT b.guild_item_id,
b.guild_id,
g.guild_name,
i.item_name,
i.item_description,
i.item_price
FROM guild_item_bridge b
INNER JOIN guild g
ON b.guild_id =
g.guild_id
INNER JOIN item i
ON b.item_id = i.item_id
WHERE (guildItemId IS NULL
OR guildItemId =
b.guild_item_id)
AND (guildId IS NULL
OR guildId =
b.guild_id);
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE;

OPEN itemCursor;

FETCH itemCursor INTO cGuildItemId,
cGuildId,
cGuildName,
cItemName,
cItemDescription,
cItemPrice;

REPEAT
SET xml =
CONCAT(xml,'',cGuildItemId,'');
SET xml = CONCAT(xml,'',cGuildId,'');
SET xml =
CONCAT(xml,'',REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(cGu
ildName,'&','&'),'>','>'),'<','<'),'''','''),'"','"'),
'
');
SET xml = CONCAT(xml,f_xmlString('item_name',cItemName));
SET xml =
CONCAT(xml,'',REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(cI
temDescription,'&','&'),'>','>'),'<','<'),'''','''),'"','&q
uot;'),'
');

SET xml =
CONCAT(xml,'',cItemPrice,'
');

FETCH itemCursor INTO cGuildItemId,
cGuildId,
cGuildName,
cItemName,
cItemDescription,
cItemPrice;
UNTIL done END REPEAT;

SET xml = CONCAT(xml,'
');

SELECT xml;

CLOSE itemCursor;
END$$
DELIMITER ;
Here's how it works: The stored procedure shown in listing 8-2 retrieves the result of a query, builds an XML string containing the opening root element, and then performs the following steps for each row retrieved:
1. If the item is numeric, concatenate it, wrapped in the appropriate XML tags, to the XML string.
2. If the item is alpha or alphanumeric, the stored function shown in Listing 8-1 is invoked to handle any entities and wrap the information in appropriate XML tags. The result of this stored function is then concatenated to the XML string
After all the rows have been processed, the closing root element is appended to the XML string and the process is complete. Now that we have a reliable source of XML, let's examine how we can use it in a web browser.
8.2.2. Data Islands for Internet Explorer
The official party line about XML Data Islands is that they are a "Microsoft-only" technology and, therefore, will not work with any other browser. Yeah, right. However, before altering the fabric of reality as only a mad scientist can, let's take a closer look at what XML data islands are and how they work.

As foreboding as the term XML Data Island is, according to the official definition, it is nothing more than XML embedded somewhere in an HTML document. Not too badsounds about as scary as a bowl of goldfish. In fact, Listing 8-3 is a basic HTML page with XML embedded smack in the middle of it, with Figure 8-3 showing what it looks like in Microsoft Internet Explorer.


Related Posts



0 comments:

Post a Comment

 

Networking

FOREX TRADER

Sexi Women

Copyright © Free ebooks Download | Powered by Blogger | Template by Blog Go Blog