Parsing from a file:
< Html > < Body > < Script Type = "Text/JavaScript" > Try // Internet Explorer {Xmldoc = New Activexobject ( " Microsoft. xmldom " );} Catch (E ){ Try // Firefox, Mozilla, opera, etc. {Xmldoc = Document. Implementation. createdocument ( "" , "" , Null );} Catch (E) {alert (E. Message )}} Try {Xmldoc. async = False ; Xmldoc. Load ( " Books. xml" ); Document. Write ( " Xmldoc is loaded, ready for use " );} Catch (E) {alert (E. Message )} </ Script > </ Body > </ Html >
Parse from string:
< Html > < Body > < Script Type = "Text/JavaScript" > Text = " <Bookstore> " Text = Text + " <Book> " ; Text = Text + " <Title> Harry Potter </title> " ; Text = Text + " <Author> j k. Rowling </author> " ; Text = Text + " <Year> 2005 </year> " ; Text = Text + " </Book> " ; Text = Text + " </Bookstore> " ; Try // Internet Explorer {Xmldoc = New Activexobject ( " Microsoft. xmldom " ); Xmldoc. async = " False " ; Xmldoc. loadxml (text );} Catch (E ){ Try // Firefox, Mozilla, opera, etc. {Parser = New Domparser (); xmldoc = Parser. parsefromstring (text, " Text/XML " );} Catch (E) {alert (E. Message)} document. Write ( " Xmldoc is loaded, ready for use " ); </ Script > </ Body > </ Html >
Next, we will operate the XML document like Dom.
More: http://www.w3school.com.cn/xmldom/dom_parser.asp