Sun River
          Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
          posts - 78,comments - 0,trackbacks - 0
          Here are some XML processing examples in Javascript. If you've got some XML data with XMLHttpRequest (there is actually no XMLHttpRequest in the examples - we create the XML object from a string - can be handy too) you need to read/convert etc. it to some other format and do something with it. Traversing a DOM/XML tree can be tricky, so have a look at these examples:
          Example #1:
          Using getElementsByTagName, we iterate thru the XML tree and read the numerous children/sibling.
          <html>
          <body>
          <script>
          var xmlstring = '<?xml version=\"1.0\"?>\
          <shoppingcart date="14-10-2005" total="123.45">\
          <item code="12345">\
          <name>Widget</name>\
          <quantity>1</quantity>\
          </item>\
          <item code="54321">\
          <name>Another Widget</name>\
          <quantity>2</quantity>\
          </item>\
          </shoppingcart>';
          // convert the string to an XML object
          var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");
          // get the XML root item
          var root = xmlobject.getElementsByTagName('shoppingcart')[0];
          var date = root.getAttribute("date");
          alert("shoppingcart date=" + date);
          var items = root.getElementsByTagName("item");
          for (var i = 0 ; i < items.length ; i++) {
          // get one item after another
          var item = items[i];
          // now we have the item object, time to get the contents
          // get the name of the item
          var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
          // get the quantity
          var quantity = item.getElementsByTagName("quantity")[0].firstChild.nodeValue;
          alert("item #" + i + ": name=" + name + " quantity=" + quantity);
          }
          </script>
          </body>
          </html>
          
          Example #2:
          Here is a more universal example with the method "childNodes".
          <html>
          <body>
          <script>
          var xmlstring = '<?xml version="1.0"?>\
          <root>\
          <data>\
          <row>\
          <cell>Admiral</cell>\
          <cell>Melon</cell>\
          <cell>Carrot</cell>\
          </row>\
          <row>\
          <cell>Captain</cell>\
          <cell>Banana</cell>\
          <cell>Zucchini</cell>\
          </row>\
          </data>\
          <data>\
          <row>\
          <cell>Midshipman</cell>\
          <cell>Orange</cell>\
          <cell>Potatoe</cell>\
          </row>\
          </data>\
          </root>';
          // convert the string to an XML object
          var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");
          // get the XML root item
          var root = xmlobject.getElementsByTagName('root')[0];
          for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
          var node = root.childNodes.item(iNode);
          for (i = 0; i < node.childNodes.length; i++) {
          var sibling = node.childNodes.item(i);
          for (x = 0; x < sibling.childNodes.length; x++) {
          var sibling2 = sibling.childNodes.item(x);
          if (sibling2.childNodes.length > 0) {
          var sibling3 = sibling2.childNodes.item(0);
          alert(sibling3.data);
          }
          }
          }
          }
          </script>
          </body>
          </html>
          
          posted on 2007-06-14 06:40 Sun River 閱讀(276) 評論(0)  編輯  收藏 所屬分類: Java Script / Ajax
          主站蜘蛛池模板: 集贤县| 南陵县| 杭锦旗| 石柱| 西昌市| 乌恰县| 丘北县| 工布江达县| 鹰潭市| 山西省| 靖宇县| 山丹县| 武穴市| 五台县| 乐亭县| 平舆县| 葵青区| 孝感市| 晋宁县| 抚顺市| 昌邑市| 黑水县| 三都| 孙吴县| 佛山市| 东源县| 凭祥市| 伊宁市| 南和县| 稷山县| 龙江县| 阿巴嘎旗| 任丘市| 呼图壁县| 盐津县| 万盛区| 青岛市| 根河市| 夹江县| 东辽县| 乌鲁木齐县|