ArticleDirectory
- Code explanation:
- Code explanation:
- Code explanation:
- Code explanation:
Browser differences in Dom Parsing
All modern browsers support W3C Dom specifications, but there are differences between browsers. There are two important differences:
- XML loading method;
- Handling of white spaces and line breaks;
1. xml loading method:
All modern browsers have built-in XML Parser for reading and operating XML. The parser reads XML into the memory and converts it to an xml dom object accessed by JavaScript. The XML Parser of Microsoft is different from the parser in other browsers. Microsoft's parser supports loading XML files and XML strings (text), while other browsers use separate Resolvers. However, all Resolvers contain functions that traverse the XML tree, access, insert, and delete nodes.
Load XML with Microsoft XML Parser
Microsoft's XML parser is built in Internet Explorer 5 and later.
The following JavaScript snippet loads the XML document ("books. xml") into the Parser:
Xmldoc = new activexobject ("Microsoft. xmldom"); xmldoc. async = "false"; xmldoc. Load ("books. xml ");
Code Explanation:
- The first line creates an empty Microsoft XML Document Object.
- Disable asynchronous loading in the second line. This ensures that the parser will not continue to execute the script until the file is fully loaded.
- The third line informs the parser to load the document named "books. xml ".
The following code loads and parses an XML 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>
Note:Loadxml ()The method is used to load a string (text), whileLoad ()Used to load files.
XML parser in Firefox and other browsers
The following JavaScript snippet loads the XML document ("books. xml") into the Parser:
Xmldoc = Document. Implementation. createdocument ("", "", null); xmldoc. async = "false"; xmldoc. Load ("books. xml ");
Code explanation:
- Create an empty XML document object in the first line
- Disable asynchronous loading in the second line. This ensures that the parser will not continue to execute the script until the file is fully loaded.
- The third line informs the parser to load the document named "books. xml ".
The following JavaScript snippet loads a string named TXT into the Parser:
Parser = new domparser (); xmldoc = parser. parsefromstring (txt, "text/XML ");
Code explanation:
- Create an empty XML document object in the first line.
- The second line tells the parser to load a string named txt
Note:Use Internet ExplorerLoadxml ()Method to parse the XML string, while other browsers useDomparserObject.
To avoid repeated code writing due to document loading, you can store the code in a separate Javascript file:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
Error: access authentication SS Domains
For security reasons, modern browsers do not allow cross-origin access.
This means that the webpage and the XML file it is trying to load must all be on the same server.
The XML file opened by the w3school instance is located in the w3school domain.
If you want to use the above example on your webpage, you must put the XML file on your server. Otherwise, xmldoc. Load () will produce the error "access is denied ".
2. Differences between blank lines and line breaks:
Firefox, and some other browsers, will treat empty blank spaces or line breaks as text nodes, and Internet Explorer will not.
<Book> <title> Harry Potter </title> <author> j k. rowling </author> <year> 2005 </year> <price> 29.99 </price> </book>
The following code snippet shows how many sub-nodes the root element of (books. XML) has:
Xmldoc = loadxmldoc ("books. xml" contains multiple xforwardedxmldoc.doc umentelement. childnodes; document. Write ("Number of child nodes:" + X. Length );
The result depends on the browser used. Firefox outputs 9 (one line break and four blank nodes are added), while ie outputs 4
Ignore empty text between nodes
To ignore empty text nodes between element nodes, check the node type. The element node type is 1:
Avoid empty text nodes
Firefox and some other browsers, empty blank or line feed whenCompositionThis node, and IE does not.
This causes a problem when using the following attributes: firstchild, lastchild, nextsibling, and previoussibling.
To avoid locating empty text nodes (spaces and line breaks between element nodes), we use a function to check the node type:
Function get_nextsibling (n) {Y = n. nextsibling; while (Y. nodetype! = 1) {Y = Y. nextsibling;} return y ;}
With the above function, we can use get_nextsibling (node) to replace the node. nextsibling attribute.
Code explanation:
The element node type is 1. If the same-level node is not an element node, move it to the next node until the element node is found. In this way, the same results can be obtained in IE and Firefox.
More