打印本文 打印本文 关闭窗口 关闭窗口
XML Basic-from w3schools.com
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4067  更新时间:2009/4/23 15:41:28  文章录入:mintao  责任编辑:mintao
lidation

If you are using JavaScript in IE 5.0, you can create an XML document object with the following code:

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")

If you are using VBScript, you create the XML document object with the following code:

set xmlDoc=CreateObject("Microsoft.XMLDOM")

If you are using VBScript in ASP, you can use the following code:

set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")


Loading an XML file into the parser

XML files can be loaded into the parser using script code.

The following code loads an XML document (note.xml) into the XML parser:

<script type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
// ....... processing the document goes here
</script>

The second line in the code above creates an instance of the Microsoft XML parser.

The third line turns off asynchronized loading, to make sure that the parser will not continue execution before the document is fully loaded.

The fourth line tells the parser to load the XML document called note.xml.


Loading pure XML text into the parser

XML text can also be loaded from a text string.

The following code loads a text string into the XML parser:

<script type="text/javascript">

var text="<note>"
text=text+"<to>Tove</to><from>Jani</from>"
text=text+"<heading>Reminder</heading>"
text=text+"<body>Don''''t forget me this weekend!</body>"
text=text+"</note>"

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(text)
// ....... processing the document goes here
</script>

Note that the "loadXML" method (instead of the "load" method) is used to load a text string.


Displaying XML with JavaScript

To display XML you can use JavaScript.

JavaScript (or VBScript) can be used to import data from an XML file and display the XML data inside an HTML page.

To see how XML and HTML complement each other this way; first look at the XML document (note.xml), then open the HTML document (note.htm) that contains a JavaScript which reads the XML file and displays the information inside predefined spans in the HTML page.

To see how it works, Try It Yourself

You can see a lot more of this kind of JavaScript in our DOM School.



XML in Real Life

back next

Some real-life examples of how XML can be used to carry information.


Example: XML News

XMLNews is a specification for exchanging news and other information.

Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages.


An example XML News document:

<?xml version="1.0" encoding="ISO-8859-1"?>

<nitf>

<head>
<title>Colombia Earthquake</title>
</head>

<body>

<body.head>
<headline>
<hl1>143 Dead in Colombia Earthquake</hl1>
</headline>
<byline>
<bytag>By Jared Kotler, Associated Press Writer</bytag>
</byline>
<dateline>
<location>Bogota, Colombia</location>
<story.date>Monday January 25 1999 7:28 ET</story.date>
</dateline>
</body.head>

</body>

</nitf>



上一页  [1] [2] [3] [4] [5] 

打印本文 打印本文 关闭窗口 关闭窗口