I put together a simple XML schema for a directory (address-book) markup system. The basic structure is as follows:
<?xml version="1.0" encoding="UTF-8">
<di开发者_JS百科rectory
xmlns="http://example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com directory.xsd">
<title>An Example Directory Document</title>
<creator>Lorem Ipsum Dolor</creator>
<date>September 29, 2011</date>
<person>
<firstName>Lorem</firstName>
<middleName>Ipsum</middleName>
<lastName>Dolor</lastName>
<email type="personal">lorem@ipsum.org</email>
<email type="other">ispum@dolor.org</email>
<phone type="mobile">(123) 456-7890</phone>
</person>
</directory>
This is the JavaScript (and E4X; this is actually only being used if a PHP $_GET[]
or $_REQUEST[]
variable is true, due to the fact that only Firefox has decent E4X support):
<script type="text/javascript;e4x=1">
var directory;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://example.com/directoryExample.xml', true);
xmlhttp.onreadystatechange = (function() {
if(xmlhttp.readyState === 4) {
if(xmlhttp.status === 200) {
directory = xmlhttp.responseXML;
HandleDirectoryData();
}
}
});
function HandleDirectoryData() {
var container = document.getElementById('output');
var directoryTitle = directory.title;
var directoryCreator = directory.creator;
var directoryDate = directory.date;
var title = document.createElement('p');
var creator = document.createElement('p');
var titleLabel = document.createElement('label');
var creatorLabel = document.createElement('label');
title.id = "xmlDirectoryTitle";
creator.id = "creator";
titleLabel.for = title.id;
creatorLabel.for = creator.id;
titleLabel.textContent = "Title:";
creatorLabel.textContent = "Creator:";
titleLabel.appendChild(title);
creatorLabel.appendChild(creator);
container.appendChild(titleLabel);
container.appendChild(creatorLabel);
for each(var person in XMLData) {
console.log("Displaying of the person data happens here...");
}
}
I believe the XMLHttpRequest is fine, and that it is simply the parsing that has something wrong with it.
Thanks in advance!
精彩评论