將 XML 新增到 XHTML 應用程式並從 XHTML 應用程式中檢索 XML
使用 XHTML 時,應該避免使用 document.write
和 innerHTML
等方法,因為它們將 XML 視為文字並且不能正確地序列化程式碼; HTML 中的 id
屬性基本上被轉儲到 DOM 中,並且 id
屬性沒有被序列化,這意味著當嘗試使用諸如 document.getElementById('example')
之類的東西引用它時,瀏覽器將不會看到id。以下示例從 XHTML 應用程式獲取獲取XHTML 程式碼並將其設定 到 XHTML 應用程式。
將 XHTML 新增到** DOM 並從 DOM 中檢索 XML**
<?php
if (isset($_SERVER['HTTP_ACCEPT']) && stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml'))
{
// Client header isset` and explicitly declares XHTML parser support.
header('Content-Type: application/xhtml+xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<!DOCTYPE html>'."\n";
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'."\n";
}
else
{
//Some browsers do not declare support, IE9 shamelessly uses `*/*` in example.
echo '<!DOCTYPE html>'."\n";
echo '<html>'."\n";
}
?>
<head>
<style type="text/css">
* {border: 0; margin: 0; padding: 0;}
</style>
<script type="application/javascript">
//<![CDATA[
function xml_get(target)
{
return new XMLSerializer().serializeToString(target)
}
function xml_set(xml,position,target)
{
if (typeof target=='string' && document.getElementById(target)) {target = document.getElementById(target);}
if (!target) {alert('Error: target element was not found in the DOM.');}
else if (position=='after')
{
if (target.nextSibling && target.nextSibling!='[object Text]') {target.insertBefore(xml.getElementsByTagName('*')[0],target.nextSibling);}
else {target.parentNode.appendChild(xml.getElementsByTagName('*')[0]);}
}
else if (position=='before') {target.parentNode.insertBefore(document.importNode(xml.getElementsByTagName('*')[0],true),target);}
else if (position=='inside') {target.appendChild(document.importNode(xml.getElementsByTagName('*')[0],true));}
else if (position=='replace') {target.parentNode.replaceChild(document.importNode(xml.getElementsByTagName('*')[0],true),target);}
else {alert('Error: unknown position to import data to: '+position+'.');}
}
window.onload = function(event)
{
var xml_after = new DOMParser().parseFromString('<p xmlns="http://www.w3.org/1999/xhtml">XML string for <em>after</em> the h1[0] element!</p>','application/xml');
var xml_before = new DOMParser().parseFromString('<p xmlns="http://www.w3.org/1999/xhtml">XML string for <em>before</em> the h1[0] element!</p>','application/xml');
var xml_inside = new DOMParser().parseFromString('<p xmlns="http://www.w3.org/1999/xhtml">XML string for <em>inside</em> inside the element with the id <code>example</code>!</p>','application/xml');
var xml_replace = new DOMParser().parseFromString('<p xmlns="http://www.w3.org/1999/xhtml">XML string for <em>replace</em> example!</p>','application/xml');
xml_set(xml_after,'after',document.getElementsByTagName('h1')[0]);
xml_set(xml_before,'before',document.getElementsByTagName('h1')[0]);
xml_set(xml_inside,'inside','example');
xml_set(xml_replace,'replace','example_replace');
alert(xml_get(document));
}
//]]>
</script>
</head>
<body>
<h1><span>XHTML and JavaScript Simple Demonstration</span></h1>
<div id="example"></div>
<div id="example_replace"></div>
</body>
</html>