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">
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 xw.xmldoc.doc umentelement. 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 // functions 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 </ Operator > < Value > 1 </ Value > </ Filter > < Filter ID = "2" > < Filtertype ID = "Ab22" > Segment </ Filtertype > < Operator ID = "1" > Equal </ Operator > < Value > 2 </ Value > </ Filter > < Logic > Or </ Logic > </ Filterrule >