I need to save the data offline, So I save the data as XML. I don't know how to get the XML ob

Source: Internet
Author: User

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  > 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.