So far Ive introduced you to XML and given you some background information on how to construct XML documents. Youve also seen how XML is different from HTML and XHTML. Now its time to explore how you can use XML documents.
Remember that the primary purpose for XML documents is the storage of data. XML allows people to share information using a self-describing document. The data is easy to read and interpret. Software packages can also read XML documents and use them as a medium for information exchange.
An XML document is portable and doesnt require the purchase of any specific software or technology. You can use the same XML documents for many different purposes. Best of all, XML documents are completely platform independent.
Common uses for XML documents include
Storing and sharing information
Querying and consuming web services
Describing configuration settings
Interacting with databases
Interacting with Office 2003 documents
The most important use for XML documents is in storing information. XML documents provide a way to describe structured data within a text file. The advantage of XML over other storage formats is that it is a standard so you can use the same content in many different ways. The same XML file could provide content for a website, a Flash movie, and a printed document. You save time because you need to create the XML file only once to use it in these varied settings.
Each XML document that you create will probably have a different structure designed to meet the needs of the people or software who will use the information. The element names will describe the data they contain, and the element structures will show how blocks of information relate to each other.
An XML document doesnt need any specific software or operating system, which means you can share it with other people and software applications. XML documents can also provide information to other web-based applications, including websites and Flash applications.
If you are working in an industry group , you can design your own language for sharing information. By creating DTDs or schemas, you ensure that everyone understands how the language works and that their XML documents conform to a standard set of rules. CML and MathML are good examples of common languages.
The listing that follows shows a MathML document taken from the examples at www.mathmlcentral.com/Tools/FromMathML.jsp. The listing shows how sin(x 2 ) could be described using MathML elements.
<math xmlns="http://www.w3.org/1998/Math/MathML"> <mrow> <mi>sin</mi> <mo>⁡</mo> <mo>(</mo> <msup> <mi>x</mi> <mn>2</mn> </msup> <mo>)</mo> </mrow> </math>
You can find out more about MathML at www.w3.org/Math/.
Another useful standard relates to graphics. Scalable Vector Graphics (SVG) is an XML-based language that describes two-dimensional graphics. If you want to find out more, the SVG recommendation is at www.w3c.org/Graphics/SVG/.
The following listing shows a sample SVG document. Ive saved the document as shapes .svg (in your resource files) if you want to have a closer look. The elements describe a yellow rectangle, blue ellipse, and green triangle.
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd"> <svg> <desc>Shapes</desc> <rect x="5" y="5" width="100" height="50" fill="yellow"/> <ellipse cx="200" cy="100" rx="100" ry="40" fill="blue"/> <polygon points="110,140 40,300 120,250" fill="green"/> </svg>
Figure 2-10 shows this file displayed in Internet Explorer.
XML documents are the standard way to share information through web services. Web services are public functions that organizations make available. For example, you can use web services to calculate currency exchange transactions, look up weather details, read news feeds, and perform searches at Amazon or Google.
When you send a request to a web service, youll often use SOAP, an XML format. Youll also receive the information from the web service in an XML document. Well look at web services in more detail later in this book.
Many software packages use XML documents to describe their configuration settings. For example, an XML document format is used to configure .NET applications. The settings for a .NET application are stored in a file called web.config . The file uses standard XML elements to store settings such as debugging, authentication, error handling, and global variables . The following listing shows a sample web.config file. You can also see the saved web.config file within your resources.
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appSettings> <add key="fileSaveLocation" value="D:\Hosting\website\images\"/> </appSettings> <system.web> <customErrors mode="Off"/> <compilation debug="true"/> </system.web> </configuration>
The file contains a global variable or key location for saving files: fileSaveLocation . There are also some settings for customized errors and debugging.
Many common databases allow you to work with XML documents. SQL Server and Oracle both offer support for XML interaction. XML documents can query a database and return results. For example, in SQL Server, you can construct a SELECT statement that returns the results as an XML fragment:
SELECT * FROM BOOKS FOR XML AUTO
XUpdate is an XML-based language that describes updates to an XML document tree. It is not a W3C recommendation but uses the XPath specification. XUpdate is one way to manage XML document and database updates. Flash uses XUpdate in the XUpdateResolver data component.
You can find out more about XUpdate at http://xmldb-org. sourceforge .net/xupdate/xupdate-wd.html. There are some useful examples of XUpdate statements at www.xmldatabases.org/projects/XUpdate-UseCases/.
One exciting new application for XML is its role in Microsoft Office 2003 documents. For PC users, this means that you can save Word, Excel, and Access 2003 documents in XML format. Office 2003 can generate XML documents that you can use in other software packages, such as Flash. You can also display and update XML documents in Office 2003.
The Save As command converts Word and Excel 2003 documents into XML format using either WordprocessingML or SpreadsheetML . These are XML-based languages created by Microsoft to describe Word and Excel structures and formatting. You can also apply your own schema so that you can modify the XML documents produced by Office 2003.
Unfortunately, this functionality is only available for PC users. There is limited XML functionality in Excel 2004 for Macintosh users. Office XP for PCs also offers some XML support, but it is limited compared with Office 2003.