Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How does js get the value of a node in xml and output it?
How does js get the value of a node in xml and output it?
1, use the childNodes attribute, and take it in order.

Implementation process: first create an xml object, then load the xml file, then determine the location of the node to be fetched according to the serial number of the parent node and its own serial number in the xml file, and finally return the value of the node to be fetched.

//pId Parent node serial number of the node to be extracted.

//cId node serial number to be extracted

Function? getXmlNodeValue(pId,cId)? {

var? xmlDoc? =? New? ActiveXObject ("Microsoft. XML DOM ");

xmlDoc.async? =? Fake;

XML doc . load(" employee info . XML ");

var? Node? =? XML doc . document element . child nodes[pId]。 child nodes[cId]; Return? nodes.childNodes[0]。 Text;

} calling method: alert (get xmlnodevalue (1, 2));

2. Use the for loop to get it.

Implementation process: first create an ie-supported? Xml object, if an exception occurs, create an empty xml object supported by FireFox and return an empty one; Then load the xml file, return null if there is an exception, and finally find the node with the same value as the incoming node through for loop traversal, and return the attribute value belonging to the node after finding it.

//nodeValue of the node that Nodevalue wants to get.

Function? getXmlNodeValueFor(nodeValue){

var? xmlDoc

Try it? {

//Create one? ie? Support? XML? Document object

xmlDoc? =? New? ActiveXObject ("Microsoft. XML DOM ");

}catch(e){

Try {

//Create an empty XML document object in FireFox.

XML doc = document . implementation . create document(""," ",null);

}catch(e){

Alert (e-mail);

Return? "";

}

}

xmlDoc.async? =? Fake;

Try it? {

XML doc . load(" employee info . XML ");

}catch(e){

Alert (e-mail);

Return? "";

}

var? xd = XML doc . document element . child nodes;

if(xd==null)?

Return? "";

var? tempValue

for(var? I = 0; I & ltxd.lengthi++){

if(xd[i]。 Child node [0]. Child node [0]. nodeValue = = nodeValue)temp value = xd[I]。 Child node [2]. Child node [0]. nodeValue

}

Return? tempValue

} Call method: alert(getXmlNodeValueFor ("Wang Jialin"));