Script code: J + XML examples of the analytical methods of operation

  Script code: Js + XML analytical examples of the methods of operation of the xml I Login.xml as follows. 
  <? Xml version = "1.0" encoding = "utf-8?"> 
<Login>
<Character>
  <C Text="热血" Value="0"> </ C> 
  <C Text="弱气" Value="1"> </ C> 
  <C Text="激情" Value="2"> </ C> 
  <C Text="冷静" Value="3"> </ C> 
  <C Text="冷酷" Value="4"> </ C> 
  </ Character> 
<Weapon>
  <W Text="光束剑" Value="0"> </ W> 
  <W Text="光束配刀" Value="1"> </ W> 
  </ Weapon> 
<EconomyProperty>
  <P Text="平均型" Value="0"> </ P> 
  <P Text="重视攻击" Value="1"> </ P> 
  <P Text="重视敏捷" Value="2"> </ P> 
  <P Text="重视防御" Value="3"> </ P> 
  <P Text="重视命中" Value="4"> </ P> 
  </ EconomyProperty> 
  </ Login> 
  Now, I need to document the contents of this xml to operate. 
  Firstly, we need to load this xml document js load xml document, and is conducted through the XMLDOM. 
  / / Load the xml document 
  LoadXML = function (xmlFile) 
  ( 
  Var xmlDoc; 
  If (window.ActiveXObject) 
  ( 
  XmlDoc = new ActiveXObject ( 'Microsoft.XMLDOM'); 
  XmlDoc.async = false; 
  XmlDoc.load (xmlFile); 
  ) 
  Else if (document.implementation & document.implementation.createDocument) 
  ( 
  XmlDoc = document.implementation.createDocument ('',''null); 
  XmlDoc.load (xmlFile); 
  ) 
  Else 
  ( 
  Return null; 
  ) 

  Return xmlDoc; 
  ) 

  Xml document object out, I will be next on the operation of the documents. 
  For example, we now need to be node Login / Weapon / W of a node's attributes, then we can then do the following. 

  / / First xml object to judge 
  CheckXMLDocObj = function (xmlFile) 
  ( 
  Var xmlDoc = loadXML (xmlFile); 
  If (xmlDoc == null) 
  ( 
  Alert ( 'Your browser does not support xml document reader, therefore prevents you from the pages of the operation, recommend using IE5.0 and above can be solved this problem!'); 
  Window.location.href = '/ Index.aspx'; 
  ) 

  Return xmlDoc; 
  ) 

  / / Then start acquiring necessary Login / Weapon / W the first node attribute value 
  Var xmlDoc = checkXMLDocObj ( '/ EBS / XML / Login.xml'); 
  Var v = xmlDoc.getElementsByTagName ( 'Login / Weapon / W') [0]. ChildNodes.getAttribute ( 'Text') 

  And I in my writing is in the process of this, of course, I am in the process of writing is the practice has been applied to it. To be out for the Show 

  InitializeSelect = function (oid, xPath) 
  ( 
  Var xmlDoc = checkXMLDocObj ( '/ EBS / XML / Login.xml'); 
  Var n; 
  Var l; 
  Var e = $ (oid); 
  If (e! = Null) 
  ( 
  N = xmlDoc.getElementsByTagName (xPath) [0]. ChildNodes; 
  L = n.length; 
  For (var i = 0; i <l; i + +) 
  ( 
  Var option = document.createElement ( 'option'); 
  Option.value = n [i]. GetAttribute ( 'Value'); 
  Option.innerHTML = n [i]. GetAttribute ( 'Text'); 
  E.appendChild (option); 
  ) 
  ) 
  ) 
  Above the access code, we are through xmlDoc.getElementsByTagName (xPath) carried out. 
  Can also xmlDoc.documentElement.childNodes (1) .. childNodes (0). GetAttribute ( 'Text') for a visit. 
  Some common methods: 
  XmlDoc.documentElement.childNodes (0). NodeName, can be the name of the node. 
  XmlDoc.documentElement.childNodes (0). NodeValue can get this node values. This value is from the xml format like this: <a> b </ b>, so this can be b value. 
  XmlDoc.documentElement.childNodes (0). HasChild can determine whether there is any node of 

  According to my experience, it is best to use getElementsByTagName (xPath) method node for a visit, because this way can be directly through xPath to locate nodes, this way there will be better performance. 

  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1661107 

  [Collections to pick my network] leon_huang published in the June 21, 2007 18:58:00 

Tags:

Releated Java Articles

Comments

Leave a Reply