블로그 이미지
Sunny's

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2009. 12. 9. 09:40 Ajax

Json -> xml
xml - > Json



<script type="text/javascript" src="inc/xml2json.js"></script>
<script type="text/javascript" src="inc/json2xml.js"></script>

<script type="text/javascript">
  /**
   * @file action
   * @brief Javascript Action 2.0
   *
   * registered date 20091209
   * updated date 20091209
   * programmed by Yong Sun. Choi. (최용선)
   * http://sunnydev.tistory.com
   */
  var m_xml = '<Pages><page><no>1</no><status>1</status></page><page><no>2</no><status>2</status></page></Pages>';

 // xmldom 로드
  var dom = parseXml(m_xml);

  // dom -> json 변형 : 문자열인 상태
  var json = xml2json(dom, "");

  // json 객체화
  json = eval("(" + json + ")");

 document.writeln(json.Pages.page[0].status);

 json.Pages.page[0].status = 5;

 document.writeln(json.Pages.page[0].status);

  // json -> dom 변형 : 문자열인 상태
  var data = json2xml(json, null);
  alert(data);


  function parseXml(xml) {
      var dom = null;
      if (window.DOMParser) {
          try {
              dom = (new DOMParser()).parseFromString(xml, "text/xml");
          }
          catch (e) { dom = null; }
      }
      else if (window.ActiveXObject) {
          try {
              dom = new ActiveXObject('Microsoft.XMLDOM');
              dom.async = false;
              if (!dom.loadXML(xml)) // parse error ..
                  window.alert(dom.parseError.reason + dom.parseError.srcText);
          }
          catch (e) { dom = null; }
      }
      else
          alert("oops");
      return dom;
  }





posted by Sunny's