Working with XML Documents

You can work with XML documents in many different ways. To start with, you need to figure out how to create the document. For example, you can write the document yourself in a text editor or have it generated automatically by a software package. You can even create a stream of XML information by running a server-side web page through a web server.

You also need to consider how to work with your XML documents. Will you view them in a web browser? Will you display and update the document in Office 2003? Maybe youll create a Flash movie that displays and updates the XML document.

Generating XML content

The simplest way to create an XML document is by typing the tags and data in your favorite text editor and saving it with an .xml extension. At the very minimum, you must follow the rules for well- formed XML content. You can also create a DTD or schema to describe the rules for your elements. This will allow you to ensure that your content is valid.

You can also use an XML editor to help create content. Many commercial XML editing tools are on the market. Search for XML editors in your favorite search engine to see a current list. I like to use XMLSpy, and Ill show you more about it in the next chapter. One advantage of XML editors is that they will color -code XML documents as well as provide code hints.

Software packages can generate XML documents automatically. Files written in PHP, ColdFusion, ASP.NET, or any other server-side language can generate XML from a database or another source. Microsoft Office 2003 for PCs also allows you to save an Office document in XML format.

Whenever you consume or use a web service, youll receive the information that you request in an XML document. A web service is like an application that you can use across the Internet. Companies like Amazon and Google make some of their services available in this way. As you can imagine, Amazon doesnt want you poking around in their database so they provide web services that allow you to carry out various searches. They protect their data but still give you access.

To use a web service, you need to send a request to the provider. The request is formatted in a specific way, and there will be different requirements for each web service that you consume. You get the results back in XML format. Well find out more about web services later in this book.

If youve used a news feed, the RSS (Rich Site Summary or Really Simple Syndication) format is an XML document. RSS uses XML to describe website changes and is really a type of web service. There are different RSS versions that you can use to provide a news feed from a website or organization. The specification for the most recent version, RSS 2.0, is at http:// blogs .law. harvard .edu/tech/rss.

One example of an RSS feed is the news service from Macromedia. You can find out about the news feed from www.macromedia.com/devnet/articles/xml_resource_feed.html and view the content at www.macromedia.com/devnet/resources/macromedia_resources.rdf. This news feed provides information from the Macromedia Developer Center using RSS 1.0.

Using XML information

Software programs access the information in an XML document through the XML Document Object Model (DOM). The DOM is a W3C specification for programming languages that you use with XML documents. At the time of writing, version 3.0 of the DOM specification was available at www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/.

The general term for any software package that processes XML documents is an XML processor . Many different software packages fall into this category. You can use them to view, extract, display, and validate your XML data. Word, Office, and Excel 2003 can exchange XML information and so are examples of XML processors.

XML parsers are one category of XML processors. Parsers can read through the content of an XML document and extract individual elements, attributes, and text. Parsers need to be able to separate processing instructions from elements, attributes, and text. Flash has a built-in XML parser that allows you to work with XML documents and include them in Flash movies.

XML parsers first check to see if a document is well formed. They cant use documents that arent well formed. Earlier we saw an error message from Internet Explorer when it tried to open a document that wasnt well formed.

XML parsers fall into two categoriesnonvalidating and validating parsers. All parsers check to see if a document is well formed. Validating parsers also compare the structure of the document with a DTD or XML schema to check that it is constructed correctly. A document that meets the rules listed within a DTD or schema is considered valid by the validating parser.

Most Web browsers are capable of displaying an XML file so that you can see the structure and content. They contain built-in parsers to process and display the XML document appropriately.

Both Internet Explorer and Mozilla Firefox contain nonvalidating XML parsers. They allow you to open and display an XML file just as you would any web page. When you open an XML file, the information structure displays using the default settings of the web browser.

Internet Explorer 6 contains version 3 of the MSXML Parser. You saw the file address.xml file as it appears when opened in Internet Explorer earlier in the chapter. Figure 2-6 shows the same file open in Firefox.

image from book
Figure 2-6: An XML file open in Firefox

You can download tools for Internet Explorer that will allow you to validate an XML file against an embedded schema. The download is called Internet Explorer Tools for Validating XML and Viewing XSLT Output ; visit the Microsoft Download Center at www.microsoft.com/downloads/search.aspx for the relevant files.

By default, the tools install in a folder called IEXMLTLS . Youll need to right-click the .inf files in the folder and choose the Install option before the tools will be available within Internet Explorer.

After you have installed the IE XML tools, you can right-click an XML page thats open in Internet Explorer. The shortcut menu will have two extra options: Validate XML and View XSL Output . Figure 2-7 shows the context menu.

image from book
Figure 2-7: After installing the Internet Explorer XML tools, you can right click in the browser window to validate the XML or view XSL output.

Firefox contains a tool called the DOM Inspector, which displays information about the structure of the XML document. Choose Tools image from book DOM Inspector to view the structure. Figure 2-8 shows this tool.

image from book
Figure 2-8: The DOM Inspector in Firefox

Flash contains a nonvalidating XML parser. This means that Flash wont check whether the XML document meets the rules set down in a DTD or XML schema. When you read in an XML document, Flash is able to convert the information into a document tree. You can then move around the tree to extract information for your Flash movie.



Foundation XML for Flash
Foundation XML for Flash
ISBN: 1590595432
EAN: 2147483647
Year: 2003
Pages: 93
Authors: Sas Jacobs

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net