varcreateXmlDom=function(){
//通过对象/属性检测法,判断是IE来是Mozilla
if (window.ActiveXObject)
{
var arrSignatures =["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0",
"MSXML2.DOMDocument.3.0",
"MSXML2.DOMDocument","Microsoft.XmlDom"];
for (var i=0; i < arrSignatures.length; i++)
{
try
{
var oXmlDom = newActiveXObject(arrSignatures[i]);
}
catch (oError)
{
}
}
}else if (document.implementation&&document.implementation.createDocument)
{
varoXmlDom=document.implementation.createDocument("","",null);
}
oXmlDom.async=false;
return oXmlDom;
}
//2.Dom对象载入xml文件有两种方式,一种是文件载入,一个是xml字符串载入
//xml文件转换为对象数组
var xmlFileToArray=function(xmlfile)
{
var oXmlDom = createXmlDom();
//oXmlDom.async = false;
try{
oXmlDom.load(xmlfile);
} catch(nerror){
//ignore
}
returnxmlToObjectArray(oXmlDom.documentElement);
}
//xml字符串转换为对象数组
var xmlStringToArray=function(xmlstring)
{
var oXmlDom = createXmlDom();
//oXmlDom.async = false;
try
{
oXmlDom.loadXML(xmlstring);
}
catch(nerror){
}
returnxmlToObjectArray(oXmlDom.documentElement);
}
//3.将dom对象装载的xml转换为数组
varxmlToObjectArray=function(xmlRoot){
var objArray = newArray;
var tNode,pNode,i,j,n=0;
vartmpString;
for(i=0;tNode=xmlRoot.childNodes[i];i++){
if(tNode.nodeType ==3 )continue;
tmpString = "var obj = newObject();";
for(j=0;pNode=tNode.childNodes[j];j++){
if(pNode.nodeType == 3 )continue;
tmpString+="obj." + getname(pNode) + "='" + getvalue(pNode) +"';";
}
eval_r(tmpString);
objArray[n++]=obj;
}
return objArray;
}
//4.获得节点名称和值
vargetname=function(pNode)
{returnpNode.attributes[0].nodeValue;}
var getvalue=function(pNode)
{returnpNode.firstChild.nodeValue;}
xml的格式:
<Records><Record><ItemName="occupation"><![CDATA[]]></Item><ItemName="businessCity"><![CDATA[]]></Item><ItemName="thumbNail"><![CDATA[]]></Item><ItemName="primaryEmail"><![CDATA[]]></Item><ItemName="businessWeb"><![CDATA[]]></Item><ItemName="homeAddr"><![CDATA[]]></Item><ItemName="businessCountry"><![CDATA[]]></Item><ItemName="editDatime"><![CDATA[2007-04-2511:17:28]]></Item><ItemName="homepage"><![CDATA[]]></Item><ItemName="Emails"><![CDATA[]]></Item><ItemName="firstname"><![CDATA[德华]]></Item><ItemName="lastname"><![CDATA[刘]]></Item><ItemName="businessAddr"><![CDATA[]]></Item><ItemName="businessProvince"><![CDATA[]]></Item><ItemName="birthDate"><![CDATA[]]></Item><ItemName="businessEmail"><![CDATA[]]></Item><ItemName="businessMobile"><![CDATA[]]></Item><ItemName="primaryMobile"><![CDATA[]]></Item><ItemName="businessPhone"><![CDATA[301-112]]></Item><ItemName="homeCountry"><![CDATA[]]></Item><ItemName="businessFax"><![CDATA[]]></Item><ItemName="homePhone"><![CDATA[]]></Item><ItemName="secondaryEmail"><![CDATA[]]></Item><ItemName="id"><![CDATA[00000000C7B9E5EFA7E8C94C8F5A8CFB4F642E78A4342100]]></Item><ItemName="company"><![CDATA[]]></Item><ItemName="homeCity"><![CDATA[]]></Item><ItemName="nickname"><![CDATA[]]></Item><ItemName="sex"><![CDATA[2]]></Item><ItemName="businessZip"><![CDATA[]]></Item><ItemName="homeProvince"><![CDATA[]]></Item><ItemName="homeZip"><![CDATA[]]></Item></Record></Records>