2014-01-16 27 views
0

我有下面xml和我需要將其形成在JavaScript,如何在javascript中形成這個xml?

<?xml version="1.0" encoding="UTF-8" ?> 
    <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <empNum>00206502</empNum> 
     <itemlist> 
      <orderid>4314</orderid> 
      <transactiondttm>2014-01-15</transactiondttm> 
     </itemlist> 
     <itemlist> 
      <orderid>413</orderid> 
      <transactiondttm>2014-01-15</transactiondttm> 
     </itemlist> 

    </Employee> 

和數量ITEMLIST節點必須被動態生成?任何想法如何完成。

+0

http://stackoverflow.com/questions/1773550/xml-json-conversion-in-javascript –

+0

是的,你可以使用循環來放置任意數量的itemlist。 – Jai

+0

你想在javscript中閱讀它,或者你想將它轉換成jvascript可讀的形式,如JSON? – Lab

回答

1

爲了形成動態的xml:

<script> 
// Simple helper function creates a new element from a name, so you don't have to add the brackets etc. 
$.createElement = function (name) { 
    return $('<' + name + ' />'); 
}; 

// JQ plugin appends a new element created from 'name' to each matched element. 
$.fn.appendNewElement = function (name) { 
    this.each(function (i) { 
     $(this).append('<' + name + ' />'); 
    }); 
    return this; 
} 

/* xml root element - because html() does not include the root element and we want to 
* include <Employee /> in the output. There may be a better way to do this. 
*/ 
var $root = $('<XMLDocument />'); 

$root.append 
(
    // one method of adding a basic structure 
    $('<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>').append 
    (

      $('<empNum />').text('1') //pass employee no 

    ) 

); 

// get a reference to Employee 
var $Employee = $root.find('Employee'); 

// get a reference to itemlist 

// or find itemlist from the $root like this: $root.find('Employee>itemlist'); 
var list = { id1: 'transactiondttm1', id2: 'transactiondttm2', id3: 'transactiondttm3', id4: 'transactiondttm4' }; // array with values 
$.each(list, function (a, b) { 
    // create 'Alice' 
    var $newitemlist = $.createElement('itemlist'); 
    // add 'orderid' element using standard jQuery 
    $newitemlist.append($('<orderid />').text(a)); 
    // add 'transactiondttm' element using our helper 
    $newitemlist.append($.createElement('transactiondttm').text(b)); 

    // add 'Alice' to <itemlist /> 
    $Employee.append($newitemlist); 
}    
); 


// display the markup as text 
alert($root.html()); 
document.write($root.html()); 
</script> 

這是如何生成的JavaScript XML/jQuery的 請試試這個,你會看到輸出

<?xml version="1.0" encoding="UTF-8"?> 
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<empnum>1</empnum> 
<itemlist> 
    <orderid>id1</orderid> 
    <transactiondttm>transactiondttm1</transactiondttm> 
</itemlist> 
<itemlist> 
    <orderid>id2</orderid> 
    <transactiondttm>transactiondttm2</transactiondttm> 
</itemlist> 
<itemlist> 
    <orderid>id3</orderid> 
    <transactiondttm>transactiondttm3</transactiondttm> 
</itemlist> 
<itemlist> 
    <orderid>id4</orderid> 
    <transactiondttm>transactiondttm4</transactiondttm> 
</itemlist> 
</employee> 

希望這可以幫助你,如果有任何疑問隨時問問題:)