I need to save the data offline, so I save the data as XML. I don't know how to get the XML object with JavaScript.

I need to save the data offline, so I save the data as XML. I don't know how to get the XML object with JavaScript.

<xml id=xmlData>

        <data>

            <tb1>

               <id>1</id>

               <name>1</name>

            </tb1>

            <tb1>

               <id>2</id>

               <name>2</name>

            </tb1>

        </data>

    </xml>

    <html id="MainForm">

    <head id="Head1">



    </head>

    <body>

    <script type="text/javascript">

    var xmlDoc;

    // code for IE

    if (window.ActiveXObject)

    {

    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

    }

    // code for Mozilla, Firefox, Opera, etc.

    else if (document.implementation.createDocument)

    {

    xmlDoc=document.implementation.createDocument("","",null);

    }

    else

    {

    alert('Your browser cannot handle this script');

    }

    xmlDoc.async=false;

    xmlDoc.load("");//how can i get the xml?



    var x=xmlDoc.documentElement.childNodes;



    for (var i=0;i<x.length;i++)

    { 

    if (x[i].nodeType==1)

      { 

      //Process only element (nodeType 1) nodes

      document.write(x[i].nodeName + ": ");

      document.write(x[i].childNodes[0].nodeValue);

      document.write("<br />");

      } 

    }

    </script>

    </body>

    </html>

 answer

try this:

ar txt='<xml id=xmlData><data><tb1><id>1</id> <name>1</name></tb1><tb1><id>2</id><name>2</name></tb1></data></xml>';



if (window.DOMParser)

{

    parser=new DOMParser();

    xmlDoc=parser.parseFromString(txt,"text/xml");

}

else // Internet Explorer

{

   xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

   xmlDoc.async=false;

   xmlDoc.loadXML(txt);

} 

var x=xmlDoc.documentElement.childNodes;



for (var i=0;i<x.length;i++)

{ 

    if (x[i].nodeType==1)

    { 

       //Process only element (nodeType 1) nodes

       console.log(x[i].nodeName + ": ");

       console.log(x[i].childNodes[0].nodeValue);

       console.log("<br />");

    } 

}

answer 2:

use a local variable in the document to do this. If you can get the XML as a string property of an object then this might be useful -

I have a somewhat similar usage in my application. I read an "XML" column from the SQL Azure DB through a service call and I get this XML data as a "string" property of the service return object.

Here is what I am doing to read that :

_LocalVariable= XMLFromString(DataObject.Filter);

                    $.each($(_LocalVariable).find("Filter"), 

                        function (index,filterDataItem) {

                        $filterDataItem =$(filterDataItem);

                        var tFilterType =$filterDataItem.find("FilterType").attr("class");

                        var tOperator = $filterDataItem.find("Operator").attr("class");

                        var tValue = $filterDataItem.find("Value").text();

                       // Do more operations

                    });





//--------------------------------------------------------------------------------

//Parse XML from String

//--------------------------------------------------------------------------------

function XMLFromString(pXMLString) {

    if (!pXMLString)

        pXMLString = "<FilterRule></FilterRule>";

    if (window.ActiveXObject) {

        var oXML = new ActiveXObject("Microsoft.XMLDOM");

        oXML.loadXML(pXMLString);

        return oXML;

    } else {

        return (new DOMParser()).parseFromString(pXMLString, "text/xml");

    }

  }

 Where my XML in the database is something like this -

<FilterRule>

  <Filter id="1">

    <FilterType id="AB11">Ranking</FilterType>

    <Operator id="1">Equal To</Operator>

    <Value>1</Value>

  </Filter>

  <Filter id="2">

    <FilterType id="AB22">Segment</FilterType>

    <Operator id="1">Equal To</Operator>

    <Value>2</Value>

  </Filter>

  <Logic>Or</Logic>

</FilterRule>

 

你可能感兴趣的:(JavaScript)