Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate XML

Status
Not open for further replies.

MikeAJ

Programmer
May 22, 2002
108
0
0
US
Hi,

I have a form and a textarea in which a user can input some XML to update a table. The column type in the database is XMLTYPE, so it must be a valid xml string going in or else an error is thrown. What's the best way to test that a string is valid XML?

Thanks,
Mike
 
...What's the best way to test that a string is valid XML?
Using one of the (excellent) server-side validators.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I tried using the built in DOM Parsers, but I'm seeing some weird behavior.

Code:
	<script type="text/javascript">
	function validateXML() {
		var xmlString = document.getElementById("src_p_xml").value;
		alert(xmlString);
		try {
			if (document.implementation.createDocument) {
				var parser = new DOMParser();
				myDocument = parser.parseFromString(xmlString, "text/xml");
			} else if (window.ActiveXObject) {
				myDocument = new ActiveXObject("Microsoft.XMLDOM")
				myDocument.async = "false";
				myDocument.loadXML(xmlString);
			}
		} catch(e) {
			alert(e);
		}
	}
	</script>

If I have valid XML, then it's fine. However, when I break the XML for testing, I don't get an alert like I would expect, but firebug catches an "unclosed token" error. Why would firebug catch it, and not the catch block?

Thanks,
Mike
 
[1] The parsing error is not catched by throwing up an e.
[2] Validating is actually meant to be restrictive sense: namely to test its well-formedness to be exact.
[tt]
function validateXML() {
var xmlString = document.getElementById("src_p_xml").value;
alert(xmlString);
try {
if (document.implementation.createDocument) {
var parser = new DOMParser();
var myDocument = parser.parseFromString(xmlString, "text/xml");
with (myDocument.documentElement) {
if (tagName=="parseerror" ||
namespaceURI=="[ignore][/ignore]") {
alert("Failed the test of well-formedness.");
return false
}
}
} else if (window.ActiveXObject) {
var myDocument = new ActiveXObject("Microsoft.XMLDOM")
myDocument.async = false
var nret=myDocument.loadXML(xmlString);
if (!nret) {
alert("Failed the test of well-formedness.");
return false
}
}
} catch(e) {
alert(e);
return true //maybe the user-agent does not support both, then it should be submitted and let server-side validation does the job
}
alert("Passed the test of well-formedness.")
return true
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top