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.