JavaScript retrieves and parses XML

Source: Internet
Author: User

XML is widely used in Web development and is usually used as a carrier for data transmission. Generally, when passing data to the front-end, it must be parsed using JavaScript before it can be used. Therefore, parsing XML with JavaScript is very common.

The following XML file is available:

<?xml version="1.0" encoding="ISO-8859-1" ?> <note><to>duncan</to> <from>John</from>  

You can use the following method for parsing:

 

XmlDoc. getElementsByTagName ("to") [0]. childNodes [0]. nodeValue how do I understand this code?

  • XmlDoc-the XML document created by the parser.
  • GetElementsByTagName ("to") [0]-the first <to> element.
  • The first child element (text node) of the childNodes [0]-<to> element ).
  • NodeValue-value of the node (text itself ).

If the xml file is:

<?xml version="1.0" encoding="ISO-8859-1" ?> <note><to>asdfsd<too>duncan1</too>    </to>     <too>duncan2</too>  <from>John</from>    

Read the first <too>: xmlDoc. getElementsByTagName ("to") [0]. getElementsByTagName ("t00") [0]. childNodes [0]. nodeValue.

Read the second <too>: xmlDoc. getElementsByTagName ("too") [0]. childNodes [0]. nodeValue.

Related Article

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.