html如何调用XML

html如何调用XML,第1张

1,内嵌XML数据

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<title>第一次亲密接触(非痞子蔡版,请不要理解为侵权)</title>

</head>

<body>

<xml id="第一次亲密接触">

<第一次亲密接触>

<对象>xml</对象>

<亲密程度>接触到XML稍微宝贵的地方---数据岛</亲密程度>

<结论>感觉不错,是个好东东</结论>

</第一次亲密接触>

</xml>

<center><h1>第一次亲密接触</h1></center>

<table border="0" datasrc="#第一次亲密接触" align="center" width="443">

<tr>

<td bgcolor="#99FF99">对象: <span datafld="对象"></span></td>

<td bgcolor="#3399CC">亲密程度:<span datafld="亲密程度"></span></td>

<td bgcolor="#CC99CC">结论: <span datafld="结论"></span></td>

</tr>

</table>

</body>

</html>

2.外部xml文件的引用

首先利用以前的知识建立一个XML文件(不是我懒,给你们一个机会锻炼^^)

涉及到以下<xml></xml>中的数据内容

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<title>第一次亲密接触(非痞子蔡版,请不要理解为侵权)</title>

</head>

<body>

<xml id="第一次亲密接触" src="firstlove.xml">

<第一次亲密接触>

<对象>xml</对象>

<亲密程度>接触到XML稍微宝贵的地方数据岛</亲密程度>

<结论>感觉不错,是个好东东</结论>

</第一次亲密接触>

</xml>

<center><h1>第一次亲密接触</h1></center>

<table border="0" datasrc="#第一次亲密接触" align="center">

<tr>

<td bgcolor="#99FF99">对象: <span datafld="对象"></span></td>

<td bgcolor="#3399CC">亲密程度:<span datafld="亲密程度"></span></td>

<td bgcolor="#CC99CC">结论: <span datafld="结论"></span></td>

</tr>

</table>

</body>

</html>

我们可以通过指定XML文件位置来引用她,一般通过src="XXX.xml"引用.

HTML文件:

<!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">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />

<title>DOMTest2 </title>

<script language="javascript">

var request = false

try {

request = new XMLHttpRequest()

} catch (trymicrosoft) {

try {

request = new ActiveXObject("Msxml2.XMLHTTP")

} catch (othermicrosoft) {

try {

request = new ActiveXObject("Microsoft.XMLHTTP")

} catch (failed) {

request = false

}

}

}

if (!request)

alert("Error initializing XMLHttpRequest!")

function callServer() {

var firstName = document.getElementById("firstName").value

var lastName = document.getElementById("lastName").value

var street = document.getElementById("street").value

var city = document.getElementById("city").value

var state = document.getElementById("state").value

var zipCode = document.getElementById("zipCode").value

var xmlString = " <profile>" +

" <firstName>" + escape(firstName) + " </firstName>" +

" <lastName>" + escape(lastName) + " </lastName>" +

" <street>" + escape(street) + " </street>" +

" <city>" + escape(city) + " </city>" +

" <state>" + escape(state) + " </state>" +

" <zip-code>" + escape(zipCode) + " </zip-code>" +

" </profile>"

// Build the URL to connect to

var url = "/Ajax/scripts/saveAddress.xml"

// Open a connection to the server

request.open("POST", url, true)

// Tell the server you're sending it XML

request.setRequestHeader("Content-Type", "text/xml")

// Set up a function for the server to run when it's done

request.onreadystatechange = updatePage

// Send the request

request.send(xmlString)

}

function updatePage() {

if (request.readyState == 4) {

if (request.status == 200) {

var xmlDoc = request.responseXML

var showElements = xmlDoc.getElementsByTagName("show")

for (var x=0x <showElements.lengthx++) {

alert("Operator XML file.")

// We know that the first child of show is title, and the second is rating

var title = showElements[x].childNodes[0].value

var rating = showElements[x].childNodes[1].value

document.getElementById("title").value=title

document.getElementById("rating").value=rating

// Now do whatever you want with the show title and ratings

}

}

else if (request.status == 404)

{ alert("Request URL does not exist")}

else if (request.status == 403) {

alert("Access denied.")

}

else

alert("Error: status code is " + request.status)

}

}

</script>

</head>

<body>

<div id="firstName">aaaa </div>

<div id="lastName">bbbb </div>

<div id="street">cccc </div>

<div id="city">dddd </div>

<div id="state">eeee </div>

<div id="">ffff </div>

<div id="zipCode">12334 </div>

<input id="title" type="text"/>

<input id="rating" type="text"/>

<input type="button" value="Click me!" onclick="callServer()" />

</body>

</html>

XMML文件:

saveAddress.xml

<?xml version="1.0" encoding="gb2312"?>

<ratings>

<show>

<title>Alias </title>

<rating>6.5 </rating>

</show>

<show>

<title>Lost </title>

<rating>14.2 </rating>

</show>

<show>

<title>Six Degrees </title>

<rating>9.1 </rating>

</show>

</ratings>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6187573.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-17
下一篇 2023-03-17

发表评论

登录后才能评论

评论列表(0条)

保存