Notes when processing XML documents using XMLHTTP

Source: Internet
Author: User

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>

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.