Here is the basic syntax of XML; I will explain the methods and properties of XML in common use with a simple example.
Series: SWF Quicker 2.0
Estimated Time of Completion: 18 minutes
1. Build XML object:
| Code: |
| var myXML = new XML(); |
2. Load XML file:
| Code: |
| myXML.load("data.xml"); |
3. Ignore white space in XML file:
| Code: |
| myXML.ignoreWhite = true;//Default setting is false. |
4. Process function of XML Object:
| Code: |
myXML.onLoad = function(success) {
if (success) {
statements;
}
} |
5. Contents of "data.XML" file:
<xml version="1.0" encoding="UTF-8" ?>
<data1 name="imagebear">
<data2 name="mariger">xiaoshandong</data2>
<data3 name="mariger3">xiaoshandong3</data3>
</data1>
6. Using trace to display the return value:
myXML.childNodes[0] will return:
<data1 name="imagebear">
<data2 name="mariger">
xiaoshandong
</data2>
<data3 name="mariger3">
xiaoshandong3
</data3>
</data1>
myXML.childNodes[0].nodeName will return:
data1
myXML.childNodes[0].attributes.name will return:
imagebear
myXML.childNodes[0].childNodes[0] will return:
<data2 name="mariger">
xiaoshandong
</data2>
myXML.childNodes[0].childNodes[0].nodeName will return:
data2
myXML.childNodes[0].childNodes[1].attributes.name will return:
mariger3
myXML.childNodes[0].childNodes[0].childNodes[0].nodeValue
xiaoshandong
| Code: |
var myName = myXML.firstChild.firstChild;
trace(myName.nodeName);//data2 will be exported.
myName = myName.nextSibling;
trace(myName.nodeName);//data3 will be exported, childNodes[0] is equal to firstChild. |
7. If you test the following codes and look at the trace value, you will understand the usage.
| Code: |
var myXML = new XML();
myXML.load("data.xml");
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if(success) {
myArray = myXML.childNodes[0];
trace(myArray.attributes.name);
trace(myXML.childNodes[0].childNodes[1].firstChild.nodeValue);
trace(myXML.firstChild);
trace(myXML.childNodes[0].nodeName);
trace(myXML.firstChild.attributes.name);
trace(myXML.childNodes[0].childNodes[1].nodeName);
trace(myXML.childNodes[0].childNodes[1].attributes.name);
trace(myXML.firstChild.firstChild);
trace(myXML.firstChild.firstChild.nodeName);
trace(myXML.firstChild.firstChild.attributes.name);
trace(myXML.childNodes[0].childNodes[0].childNodes[0].nodeValue);
var myName = myXML.firstChild.firstChild;
trace(myName.nodeName);
myName = myName.nextSibling;
trace(myName.nodeName);
}
}; |
Hopefully it is helpful to you!
Link:
http://www.sothink.com/tutorials/xml-basics.htm
Nhận xét
Đăng nhận xét
Cảm ơn