-
Notifications
You must be signed in to change notification settings - Fork 0
/
XML
56 lines (45 loc) · 1.27 KB
/
XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//跨浏览器返回XML DOM对象
function getXMLDOM(xmlStr) {
var xmlDom = null;
if (typeof window.DOMParser != 'undefined') {
xmlDom = (new DOMParser).parseFromString(xmlStr, 'text/xml');
var errors = xmlDom.getElementsByTagName('parsererror');
if (errors.length > 0) {
throw new Error('错误信息:' + errors[0].textContent);
}
} else if (typeof window.ActiveXObject != 'undefined') {
var version = [
'MSXML2.DOMDocument6.0',
'MSXML2.DOMDocument3.0',
'MSXML2.DOMDocument'
];
for (var i = 0; i < version.length; i ++) {
try {
var xmlDom = new ActiveXObject(version[i]);
} catch (e) {
//跳过
}
}
xmlDom.loadXML(xmlStr);
if (xmlDom.parseError != 0) {
throw new Error('错误信息:' + xmlDom.parseError.reason);
}
return xmlDom;
} else {
throw new Error('您的系统或浏览器不支持XML DOM对象!');
}
return xmlDom;
}
//序列化XML
function serializerXML(xmlDom) {
var xml = '';
if (typeof window.XMLSerializer != 'undefined') {
xml = (new XMLSerializer()).serializeToString(xmlDom);
} else if (typeof xmlDom.xml != 'undefined') {
xml = xmlDom.xml;
}
return xml;
}
var xmlStr = '<root><user>Lee</user></root>';
var xmlDom = getXMLDOM(xmlStr);
alert(serializerXML(xmlDom));