The XML object obtained by XMLHTTP requires different processing details according to the XML document format during parsing. Here is the point of attention in the programming process.
Pay attention to the XML document format. Note the differences between elements <.../> and attribute name = "value.
Different methods must be used for parsing based on different XML document formats. The element is element, and the attribute is attribute.
A type of data. Some parts of it can be arranged as element or attribute. The resolution method varies depending on the arrangement.
For example, the following XML Parsing Method
----------------- Menu. XML is here ---------------
<? XML version = "1.0" standalone = "yes"?>
<Menu>
<Item xbm = "0" xbmc = "unknown gender"/>
<Item xbm = "1" xbmc = "male"/>
<Item xbm = "2" xbmc = ""/>
<Item xbm = "9" xbmc = "unspecified gender"/>
</Menu>
-------------------- JavaScript -----------------------------------------
<Script language = "JavaScript" type = "text/JavaScript">
// --------------------- Test function 1 -----------------------
Function func1 ()
{
VaR XMLHTTP = new activexobject ("msxml2.xmlhttp. 3.0 ");
XMLHTTP. Open ("get", "http: // localhost/menu. xml % 22, false );
XMLHTTP. Send ();
VaR xmldoc = XMLHTTP. responsexml;
VaR root = xmldoc.doc umentelement;
Alert (root. XML );
VaR DATA = root. getelementsbytagname ("item ");
For (VAR I = 0; I <data. length; I ++)
{
VaR item = data [I];
Alert ("gender code =" + item. attributes [0]. nodevalue + "\ r \ n" + "Gender name =" + item. attributes [0]. nodename );
}
}
</SCRIPT>
If the XML format is:
<? XML version = "1.0" standalone = "yes"?>
<Menu>
<Item xbm = "0" xbmc = "unknown gender"/>
<Item xbm = "1" xbmc = "male"/>
<Item xbm = "2" xbmc = ""/>
<Item xbm = "9" xbmc = "unspecified gender"/>
</Menu>
Resolution method:
<Script language = "JavaScript" type = "text/JavaScript">
// --------------------- Test function 1 -----------------------
Function func1 ()
{
VaR XMLHTTP = new activexobject ("msxml2.xmlhttp. 3.0 ");
XMLHTTP. Open ("get", "http: // localhost/dm_xb.xml % 22, false );
XMLHTTP. Send ();
VaR xmldoc = XMLHTTP. responsexml;
VaR DATA = xmldoc. getelementsbytagname ("data ");
For (VAR I = 0; I <data. length; I ++)
{
VaR item = data [I];
VaR xbm = item. getelementsbytagname ("xbm") [0]. firstchild. Data;
VaR xbmc = item. getelementsbytagname ("xbmc") [0]. firstchild. Data;
Alert ("gender code =" + xbm + "\ r \ n" + "Gender name =" + xbmc );
}
}
</SCRIPT>