<head>
<script type="text/javascript">
function CreateMSXMLDocumentObject () {
if (typeof (ActiveXObject) != "undefined") {
var progIDs = [
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
];
for (var i = 0; i < progIDs.length; i++) {
try {
return new ActiveXObject(progIDs[i]);
} catch(e) {};
}
}
return null;
}
function BuildXMLFromString (text) {
var message = "";
if (window.DOMParser) { // all browsers, except IE before version 9
var parser = new DOMParser();
try {
xmlDoc = parser.parseFromString (text, "text/xml");
} catch (e) {
// if text is not well-formed,
// it raises an exception in IE from version 9
alert ("XML parsing error.");
return false;
};
}
else { // Internet Explorer before version 9
xmlDoc = CreateMSXMLDocumentObject ();
if (!xmlDoc) {
alert ("Cannot create XMLDocument object");
return false;
}
xmlDoc.loadXML (text);
}
var errorMsg = null;
if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
+ " at line " + xmlDoc.parseError.line
+ " at position " + xmlDoc.parseError.linepos;
}
else {
if (xmlDoc.documentElement) {
if (xmlDoc.documentElement.nodeName == "parsererror") {
errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
}
}
else {
errorMsg = "XML Parsing Error!";
}
}
if (errorMsg) {
alert (errorMsg);
return false;
}
alert ("Parsing was successful!");
return true;
}
function TestContent1 () {
var xmlText = "<root><fruit color='red'></root>";
BuildXMLFromString (xmlText);
}
function TestContent2 () {
var xmlText = "<root><fruit color='red'></fruit></root>";
BuildXMLFromString (xmlText);
}
</script>
</head>
<body>
<span><root><fruit color='red'></root></span><br />
<button οnclick="TestContent1 ()">Build XML</button>
<br /><br />
<span><root><fruit color='red'></fruit></root></span><br />
<button οnclick="TestContent2 ()">Build XML</button>
<div id="info"></div>
</body>
<script type="text/javascript">
function CreateMSXMLDocumentObject () {
if (typeof (ActiveXObject) != "undefined") {
var progIDs = [
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
];
for (var i = 0; i < progIDs.length; i++) {
try {
return new ActiveXObject(progIDs[i]);
} catch(e) {};
}
}
return null;
}
function BuildXMLFromString (text) {
var message = "";
if (window.DOMParser) { // all browsers, except IE before version 9
var parser = new DOMParser();
try {
xmlDoc = parser.parseFromString (text, "text/xml");
} catch (e) {
// if text is not well-formed,
// it raises an exception in IE from version 9
alert ("XML parsing error.");
return false;
};
}
else { // Internet Explorer before version 9
xmlDoc = CreateMSXMLDocumentObject ();
if (!xmlDoc) {
alert ("Cannot create XMLDocument object");
return false;
}
xmlDoc.loadXML (text);
}
var errorMsg = null;
if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
+ " at line " + xmlDoc.parseError.line
+ " at position " + xmlDoc.parseError.linepos;
}
else {
if (xmlDoc.documentElement) {
if (xmlDoc.documentElement.nodeName == "parsererror") {
errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
}
}
else {
errorMsg = "XML Parsing Error!";
}
}
if (errorMsg) {
alert (errorMsg);
return false;
}
alert ("Parsing was successful!");
return true;
}
function TestContent1 () {
var xmlText = "<root><fruit color='red'></root>";
BuildXMLFromString (xmlText);
}
function TestContent2 () {
var xmlText = "<root><fruit color='red'></fruit></root>";
BuildXMLFromString (xmlText);
}
</script>
</head>
<body>
<span><root><fruit color='red'></root></span><br />
<button οnclick="TestContent1 ()">Build XML</button>
<br /><br />
<span><root><fruit color='red'></fruit></root></span><br />
<button οnclick="TestContent2 ()">Build XML</button>
<div id="info"></div>
</body>