03 XML 解析
一、解析 XML
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
二、解析 XML 字符串
var xmlStr=
"<note> " +
"<to>Tove</to> " +
"<from>Jani</from> " +
"<heading>Reminder</heading> " +
"<body>Don't forget me this weekend!</body> " +
"</note>"
parser=new DOMParser();
xmlDoc=parser.parseFromString(xmlStr,"text/xml");
三、XML DOM
<script type="text/javascript">
window.onload = function () {
var xmlStr=
"<note id='n1'> " +
"<to>Tove</to> " +
"<from>Jani</from> " +
"<heading>Reminder</heading> " +
"<body>Don't forget me this weekend!</body> " +
"</note>"
parser=new DOMParser();
xmlDoc=parser.parseFromString(xmlStr,"text/xml");
var noteID = xmlDoc.getElementsByTagName("note")[0].id
var to = xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
var from = xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue
var writeStr = " note_id : " + noteID + "<br/>to : " + to + "<br/>from : " + from
document.write(writeStr)
}
</script>