tailieunhanh - Học Actionscript 3.0 - p 43
Writing XML Writing XML You’ve already seen how to write XML when creating an instance of the XML class, but you may also have to write to the instance over time. For example, you may need to add to XML based on user input or as data changes. The majority of techniques for adding content to XML mirror the process of reading the data, except this time you’re assigning information to a node rather than retrieving it. In this section, you’ll re-create the data used throughout the “Reading XML” section of this chapter. For simplicity, we’ll create element nodes, text nodes, and. | Writing XML Writing XML You ve already seen how to write XML when creating an instance of the XML class but you may also have to write to the instance over time. For example you may need to add to XML based on user input or as data changes. The majority of techniques for adding content to XML mirror the process of reading the data except this time you re assigning information to a node rather than retrieving it. In this section you ll re-create the data used throughout the Reading XML section of this chapter. For simplicity we ll create element nodes text nodes and attributes in one example and build an XML instance as if we were writing it over time. In a real scenario you would not assemble XML in multiple steps in the same script. However assuming the premise that we re writing the object in stages will allow us to demonstrate the most common XML writing methods. Because we re intentionally writing the XML out of order to demonstrate methods like insertChildBefore that will alter the order of nodes we ll show the progress of the XML as we go. The code that follows can be found in the source file. For clarity only the last trace statement is used in the source file to show the final XML content but you can uncomment any trace along the way to see interim results. To begin we must have an XML instance and a root node so line 1 creates both. Note that when adding element nodes without content you must specify a self-closing tag so the XML remains well formed. As soon as you add content to a self-closing node ActionScript will replace it with balanced open and closing tags. For example we re initially adding book as the root node but after adding the next element node the root node will become book book . Line 2 demonstrates the simplest technique for creating both an element node and a text node. When assigning a value to a node if the node does not already exist it will be created. If you assign another element node to the new node a nested element .
đang nạp các trang xem trước