XML (3), xml

Source: Internet
Author: User

XML (3), xml
 Worker displays XML using XSLT

 

--------------------------------------------------------------------------------

 

By using XSLT, you can convert an XML document into HTML format.

 

 

--------------------------------------------------------------------------------

 

Display XML with XSLT

XSLT is the preferred XML style table language.

 

XSLT (eXtensible Stylesheet Language Transformations) is far better than CSS.

 

XSLT converts an XML file to HTML before the browser displays it:

 

Display XML with XSLT

 

 

 

--------------------------------------------------------------------------------

 

Use XSLT to convert XML on the server

In the preceding example, when the browser reads an XML file, XSLT conversion is completed by the browser.

 

When using XSLT to convert XML, different browsers may produce different results. To reduce this problem, you can perform XSLT conversion on the server.

If you want to learn about XSLT, find the XSLT tutorial on our homepage.

 

 

 

 

XMLHttpRequest object

 

--------------------------------------------------------------------------------

 

XMLHttpRequest object

The XMLHttpRequest object is used to exchange data with the server in the background.

 

XMLHttpRequest object is a developer's dream, because you can:

 

Update the webpage without reloading the page

Request data from the server after the page has been loaded

Receive data from the server after the page is loaded

Send data to the server in the background

Create an XMLHttpRequest object

All modern browsers (IE7 +, Firefox, Chrome, Safari, and Opera) have built-in XMLHttpRequest objects.

 

Syntax for creating an XMLHttpRequest object:

 

Xmlhttp = new XMLHttpRequest ();

ActiveX objects are used in earlier versions of Internet Explorer (IE5 and IE6:

 

Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");

XML Parser

 

--------------------------------------------------------------------------------

 

All modern browsers have built-in XML parser.

 

The XML Parser converts an XML document to an xml dom object-an object that can be operated by JavaScript.

 

 

--------------------------------------------------------------------------------

 

Parse XML documents

The following code snippet resolves the XML document to the xml dom object:

 

If (window. XMLHttpRequest)

{// Code for IE7 +, Firefox, Chrome, Opera, Safari

Xmlhttp = new XMLHttpRequest ();

}

Else

{// Code for IE6, IE5

Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");

}

Xmlhttp. open ("GET", "books. xml", false );

Xmlhttp. send ();

XmlDoc = xmlhttp. responseXML;

 

--------------------------------------------------------------------------------

 

Parse XML strings

The following code snippet parses an XML string into an xml dom object:

 

Txt = "<bookstore> <book> ";

Txt = txt + "<title> Everyday Italian </title> ";

Txt = txt + "<author> Giada De Laurentiis </author> ";

Txt = txt + "<year> 2005 </year> ";

Txt = txt + "</book> </bookstore> ";

 

If (window. DOMParser)

{

Parser = new DOMParser ();

XmlDoc = parser. parseFromString (txt, "text/xml ");

}

Else // Internet Explorer

{

XmlDoc = new ActiveXObject ("Microsoft. XMLDOM ");

XmlDoc. async = false;

XmlDoc. loadXML (txt );

}

Note: Internet Explorer uses the loadXML () method to parse XML strings, while other browsers use DOMParser objects.

 

 

--------------------------------------------------------------------------------

 

Cross-origin access

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.

 

XML DOM

 

--------------------------------------------------------------------------------

 

DOM (Document Object Model) defines standard methods for accessing and operating documents.

 

 

--------------------------------------------------------------------------------

 

XML DOM

Xml dom (XML Document Object Model) defines standard methods for accessing and operating XML documents.

 

Xml dom views XML documents as a tree structure.

 

All elements can be accessed through the DOM tree. You can modify or delete their content and create new elements. Elements, their text, and their attributes are considered as nodes.

 

 

 

--------------------------------------------------------------------------------

 

HTML DOM

Html dom defines standard methods for accessing and operating HTML documents.

 

All HTML elements can be accessed through html dom.

 

 

 

--------------------------------------------------------------------------------

 

Load an XML file-cross-browser instance

The following example parses the XML document ("note. xml") into the xml dom object, and then extracts some information through JavaScript:

 

Instance

<Html>

<Body>

<H1> W3Schools Internal Note

<Div>

<B> To: </B> <span id = "to"> </span> <br/>

<B> From: </B> <span id = "from"> </span> <br/>

<B> Message: </B> <span id = "message"> </span>

</Div>

 

<Script>

If (window. XMLHttpRequest)

{// Code for IE7 +, Firefox, Chrome, Opera, Safari

Xmlhttp = new XMLHttpRequest ();

}

Else

{// Code for IE6, IE5

Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");

}

Xmlhttp. open ("GET", "note. xml", false );

Xmlhttp. send ();

XmlDoc = xmlhttp. responseXML;

 

Document. getElementById ("to"). innerHTML =

XmlDoc. getElementsByTagName ("to") [0]. childNodes [0]. nodeValue;

Document. getElementById ("from"). innerHTML =

XmlDoc. getElementsByTagName ("from") [0]. childNodes [0]. nodeValue;

Document. getElementById ("message"). innerHTML =

XmlDoc. getElementsByTagName ("body") [0]. childNodes [0]. nodeValue;

</Script>

 

</Body>

</Html>

 

--------------------------------------------------------------------------------

 

Important Notes!

To extract the text "Tove" from the <to> element of the preceding XML file ("note. xml"), the syntax is:

 

GetElementsByTagName ("to") [0]. childNodes [0]. nodeValue

Note that even if the XML file contains only one <to> element, you must specify an array index [0]. This is because the getElementsByTagName () method returns an array.

 

 

--------------------------------------------------------------------------------

 

Load an XML string-cross-browser instance

The following example parses the XML string to the xml dom object and extracts some information through JavaScript:

 

Instance

<Html>

<Body>

<H1> W3Schools Internal Note

<Div>

<B> To: </B> <span id = "to"> </span> <br/>

<B> From: </B> <span id = "from"> </span> <br/>

<B> Message: </B> <span id = "message"> </span>

</Div>

 

<Script>

Txt = "<note> ";

Txt = txt + "<to> Tove </to> ";

Txt = txt + "<from> Jani </from> ";

Txt = txt + "

Txt = txt + "<body> Don't forget me this weekend! </Body> ";

Txt = txt + "</note> ";

 

If (window. DOMParser)

{

Parser = new DOMParser ();

XmlDoc = parser. parseFromString (txt, "text/xml ");

}

Else // Internet Explorer

{

XmlDoc = new ActiveXObject ("Microsoft. XMLDOM ");

XmlDoc. async = false;

XmlDoc. loadXML (txt );

}

 

Document. getElementById ("to"). innerHTML =

XmlDoc. getElementsByTagName ("to") [0]. childNodes [0]. nodeValue;

Document. getElementById ("from"). innerHTML =

XmlDoc. getElementsByTagName ("from") [0]. childNodes [0]. nodeValue;

Document. getElementById ("message"). innerHTML =

XmlDoc. getElementsByTagName ("body") [0]. childNodes [0]. nodeValue;

</Script>

</Body>

</Html>

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.