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

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.


        
            
               1
               1
            
            
               2
               2
            
        
    
    
    

    
    
    
    
    

 answer

try this:

ar txt='<xml id=xmlData><data><tb1><id>1id> <name>1name>tb1><tb1><id>2id><name>2name>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("
/>"); } }

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 = "";
    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">RankingFilterType>
    <Operator id="1">Equal ToOperator>
    <Value>1Value>
  Filter>
  <Filter id="2">
    <FilterType id="AB22">SegmentFilterType>
    <Operator id="1">Equal ToOperator>
    <Value>2Value>
  Filter>
  <Logic>OrLogic>
FilterRule>

 

转载于:https://www.cnblogs.com/willsonchan/archive/2013/05/06/3063163.html

你可能感兴趣的:(I need to save the data offline, so I save the data as XML. I don't know how to get the XML object w...)