Learn flash a little every day (3) read XML from outside flash

Source: Internet
Author: User

I just learned something about as2.0 and hope to record what I learned as a good habit of learning .. Today, I started to write some very common things about flash. Beginners may not write well. For external reading of XML, flash provides a dedicated class for us. We only need to call some functions to find it easy to use XML: It is difficult to start with everything, we can first imitate a pattern to write: first look at some basic routines for calling XML in Flash: Now let's start our first step: Suppose you have written the XML file, the current job is to write our code in the FLA file to simply write an XML file, the simpler the better. create a new text named mymenu. XML. The Code is as follows: Save it as Unicode encoding (generally ANSI encoding is used by default). You can test the differences between different encodings. The file is as follows <? XML version = "1.0" encoding = "UTF-8"?> <Item> <B name = "Tree man in summer"/> </item> Question 1: How to read external XML files? Answer: the first thing we know is how to help us do this? The answer is "object ". The object-oriented thinking tells us that we should regard all this as an object. This object is used to help us process events and complete our work. First, Step 1: Create an object using the new XML () method to create an object. The Code is as follows: var myxml: xml = New XML (); Step 2: myxml. load ("mymenu. XML "); // J loads the XML file myxml. onload = function (SUCCESS) {If (SUCCESS) trace ("successful")} If the test succeeds. In general, the tracing information will appear on the output panel ." "Two words of success", this kind of work is really good for our next step. Tip: there are many similar writing methods in flash code, and a new object needs to be created. For example, VAR myxml = New XML (); var mysound = new sound (); vaR mylv = new loadvars (); and so on. Question 2: How can we read XML files? How to display it? I have learned a lot about the XML and want to know the answer. If the code is successfully debugged, we have to do something. Now let's test it: for the same code, let's read the content of a name in XML. <? XML version = "1.0" encoding = "UTF-8"?> <Item> <B name = "Summer tree man"/> </item> the code for reading name content is as follows: var myxml: xml = New XML (); system. usecodepage = true; myxml. ignorewhite = true; myxml. load ("mymenu. XML "); myxml. onload = function (SUCCESS) {If (SUCCESS) trace ("Success 1"); trace (myxml. firstchild. childnodes [0]. attributes. name);} I added a statement myxml here. firstchild. childnodes [0]. attributes. name. This indicates the variable value of the attribute in an array set under the first son. Simply put, the attribute value of the name under the tag <B name = "Summer tree man"/> included in the tag <item>. For details, refer to reading XML attributes. Our help document has the following problem: Can I read the XML value and apply it to it? We cannot always use trace to trace our information. Will it be displayed in our scenario? Now let's use text to display the name = "Summer tree man" information. Step 1: create a dynamic text named mytext. The Code is as follows var myxml: xml = New XML (); system. usecodepage = true; // prevent garbled myxml. ignorewhite = true; myxml. load ("mymenu. XML "); myxml. onload = function (SUCCESS) {If (SUCCESS) trace ("Success 1"); mytext. TEXT = myxml. firstchild. childnodes [0]. attributes. name; trace (myxml. firstchild. childnodes [0]. attributes. name);} Here I just added a mytext. TEXT = myxml. firstchild. childnodes [0]. attribut Es. Name; the purpose is to display XML Information in dynamic text, so as to achieve our goal. Conclusion: starting with the simplest method, the simpler the better, so that we do not have to increase the burden. Dynamic text can be displayed, and the content can be displayed. Our goal has been achieved. We get what we need. Question 4: How can we play with XML .? Is it a little excited to learn the above? If so, now I want to raise a question: what is the definition of array? What is the role of arrays? Can it be hooked up with XML? What problems will occur when used with XML? Please take this question into the next stage of study and discussion. The level of the article is limited. I hope you can forgive me: the last point is to refer to the XML data operations. In the next section, I will discuss the syntax reference based on my ideas.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.