|Page (1) of 1 - 05/12/10||email article||print page|
Real-World XML: Part 9 of 12Serializing XML To A String
XML technologies offer web developers and designers more flexibility than ever before. In Real-World XML, industry expert Joe Marini covers the best programming practices with XML, including the tools needed to build effective XML structures. He demonstrates the implementation of XML formats, how these formats work in real-world situations, and how they can facilitate project planning and development. Exercise files accompany the course.
XML Essential Training is a prerequisite for getting the most out of this course.
- Understanding the Sitemap index format
- Integrating XML and design
- Using XML effectively in Firefox and Internet Explorer
- Avoiding common design mistakes
- Understanding and implementing DOM algorithms
- Building an XML tag set
- Using XML with RSS and Atom
- Processing XML data with XSLT
Serializing XML To A String
Now as I demonstrated in the previous examples both Firefox and Internet Explorer have a notion of serializing XML to a string and serializing is basically the process of converting an XML document to a format that can be saved like a string.
Now serialization can take other forms as well, but string is the most common, especially in most of the real world scenarios you will probably run into. And you would like to do this for several reasons. First, you can use it for saving XML content either to a file or some other persistent method, whether it's a stream or something like that.
And you also might want to do this to aid in debugging. So if you are working with XML and you've got a whole bunch of logic, there will probably come times when you want to display some debugging messages that contain XML content and in order to do that you need to serialize the XML to a string before you can write it out to a console or display it in a alert or whatever debugging method you use.
And as I showed, Firefox and Internet Explorer both have ways of serializing strings. Firefox provides support for an object called the XMLSerializer and you just call the serializeToString method on that object. Whereas Internet Explorer makes things really easy, it just simply provides an XML property on each XML node in the document, which provides a text representation of the document tree starting at that node.
Click Play or press spacebar to start or stop video
And just to review, to perform Firefox serialization to a string, you can see in the top example we've got a document fragment where we create a paragraph element with some text inside of it and to serialize it out, we instantiate a new XMLSerializer object and call the serializeToString function with the document as the argument.
And as I mentioned earlier that argument to serializeToString, this can be any node. It doesn't have to be the document. You can serialize starting at any node in the tree. So we could have passed in the paragraph or the text or anything like that.
And in the Internet Explorer case we use the XML property on the XML document, and again you can use this on any node. It doesn't have to be the document, but this is how you get the string representation of XML in Internet Explorer.
Okay, so now that we have covered using XML in Firefox and IE and we have seen how to serialize the data, it's time to move on to our next lesson.
www.lynda.com is an award-winning provider of educational materials, including Hands-On Training instructional books, the Online Training Library, CD- and DVD-based video training, and events for creative designers, instructors, students, and hobbyists.
The lynda.com Online Training Library and CD-ROM titles include such subjects as Photoshop, Flash, Dreamweaver, Illustrator, Office, digital photography, Web design, digital video, and many others. lynda.com's all-star team of trainers and teachers provides comprehensive and unbiased movie-based training to an international membership of tens of thousands of subscribers. Considering the speed at which technology evolves, the Online Training Library is a great solution for keeping your skills current. Library subscriptions begin as low as $25 a month, with no long-term commitment required.
Joe Marini is the director for Microsoft's Visual Studio Industry Partner program, and has been active in the Web and graphics industries for more than 15 years. He was an original member of the Dreamweaver engineering team at Macromedia, and has also held prominent roles in creating products such as QuarkXPress, mFactory's mTropolis, and Extensis QX-Tools. He is regularly a featured speaker at industry conferences and has authored or co-authored several books on Web development. His book The Document Object Model is widely regarded as the definitive resource for working with the DOM.
Related Keywords:xml, Extensible Mark-Up Language, xml training, xml tutorial, xml techniques, xml design, web design, web site design, serializing xml, xml strings