zoukankan      html  css  js  c++  java
  • js实现xml转化成字符串

     xml字符串转xml dom
    经常遇到在js里面需要解析xml的问题,然而有时候,后台返回的不是dom 而是string 字符串,需要将字符串转换成dom对象,然后才可以进行节点值解析和读取

    function createXml(str)
    {
      if(document.all)
          {
            var xmlDom=new ActiveXObject("Microsoft.XMLDOM")
            xmlDom.loadXML(str)
            return xmlDom
      }
      else
          {
             return new DOMParser().parseFromString(str, "text/xml")
      }

    如果在js端是读取文件,那就更方便了
           var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
      xmlDoc.async = false;
      xmlDoc.load("文件路径");
    2. xml dom转xml字符串
    function xmlToString(xmlObj)
    {
        if(document.all) //IE浏览器
        {
            return xmlObj.xml;
        }
        else //其他浏览器
        {
            return (new XMLSerializer()).serializeToString(xmlObj);
        }
    }
    3. 加载xml文件
    function loadXML()
    {
            try //Internet Explorer
           {
              xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
           }
            catch(e)
           {
              try //Firefox, Mozilla, Opera, etc.
            {
                xmlDoc=document.implementation.createDocument("","",null);
            }
          catch(e)
          {
              alert(e.message)}
          }
            try
          {
              xmlDoc.async=false;
              xmlDoc.load("/example/xdom/books.xml");
              document.write("xmlDoc is loaded, ready for use");
          }
            catch(e)
            {
                alert(e.message)
            }
    }

  • 相关阅读:
    《Java面向对象编程》
    大学计算机基础考试系统(CS)
    企业物资管理系统
    IP.21出现的错误
    假如你的年龄超过了23···
    如何经营婚姻
    一张舞女图测试你的左右脑切换能力【我看到左右都转呢~】
    无法加载DLL(oci.dll)
    mongodb安装信息及有关命令
    loaded the "controller" nib but the view outlet was not set.问题解决
  • 原文地址:https://www.cnblogs.com/lovedream/p/4088700.html
Copyright © 2011-2022 走看看