我想下面的XML內容解析成csv格式:讀取XML文件轉換成CSV格式
<order>
<item>abc</item>
<item>123</item>
<item/>
<item>000</item>
</order>
<order>
<item>999</item>
<item>333</item>
</order>
我能夠讀取使用FileApi XML文件。變量text
包含xml內容。您能否提供一些關於如何將每個<order>
標籤解析到數組中的見解?
<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#fileUpload").change(function() {
for (var i = 0; i < this.files.length; i++) {
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result;
}
reader.readAsText(this.files[i]);
//TODO
//1. find tag <order>
//2. get all <item> text into an array
//3. separate array elements with a semicolon
//4. show array content in the div dataContainer + <br>
//5. repeat until there is no more <order> tags to parse
}
});
});
</script>
</head>
<body>
<input type="file" id="fileUpload" multiple="multiple" />
<div id="dataContainer"></div>
</body>
</html>
這是要顯示在div dataContainer
預期輸出:
abc;123;;000
999;333