[英]Error appears when trying to parse XML file using Javascript/Jquery
[英]Trying to parse an rtf file using javascript
我有一個 rtf 文件可以上傳,一旦上傳,我希望它被解析並轉換為 json 文件。 為此,我主要使用 angular。 我的上傳工作正常,我只是在解析時遇到問題。 該文件確實得到了清理,但它沒有正確格式化為 json 文件。有人對最好的做法有什么建議嗎?
this.parseFile=function(file, uploadUrl){
reader.onload = function(onLoadEvent){
$rootScope.$apply(function(){
var dataURL = reader.result;
setUpData(dataURL);
});
};
reader.readAsText(file);
};
function saveFile(file){
var fd = new FormData();
fd.append('save_file', file);
$http({
method: 'POST',
url: "includes/loadFile.php",
data: fd,
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function (data, status, headers, config) {
msg = {
title: "success",
cls: "success",
msg: data
};
popUp(msg);
})
.error(function (data, status, header, config) {
msg = {
title: "error",
cls: "danger",
msg: data
};
popUp(msg);
});
};
function setUpData(data)
{
text = data;
text = text.replace('/[\p{Z}\s]{2,}/u', ' ');
// skip over the heading stuff
j= text.indexOf('{',1); // skip ahead to the first part of the header
var loc = 1;
var t ="";
var ansa="";
len = text.length;
getpgraph(); // skip by the first paragrap
while(j<len) {
c = text.substr(j,1);
if (c=="\\") {
// have a tag
var tag = gettag();
if (tag.length > 0) {
// process known tags
switch (tag) {
case 'par':
ansa+="\r\n";
break;
// ad a list of common tags
// parameter tags
case 'spriority1':
case 'fprq2':
case 'author':
case 'operator':
case 'sqformat':
case 'company':
case 'xmlns1':
case 'wgrffmtfilter':
case 'pnhang':
case 'themedata':
case 'colorschememapping':
var tt = gettag();
break;
case '*':
case 'info':
case 'stylesheet':
// gets to end of paragraph
j--;
getpgraph();
default:
// ignore the tag
}
}
} else {
ansa += c;
}
j++;
};
ansa = ansa.replace(/{|}|"|( | )\r/g,'');
ansa = ansa.replace(/Amount After|Amt After/g,'');
ansa = ansa.replace(/negetive amount means Refund NOTE: Layaways are not in Sales Report till they're paid off/g,'');
ansa = ansa.replace("On Line Vendor Sales Quick Report by Month",'');
ansa = ansa.replace(/ ( ) /g,'');
ansa.trim();
ansa.split("\n");
console.log(ansa);
var newData = new Array();
var vendor = new Array();
var orders = new Array();
var v = 0;
//Lets look for the line that has the word vendor
vendorLine = ansa.match(/Vendor/gi);
//Look for each order for the vendor
orderLine = ansa.match(/date/gi);
figuresLine = ansa.match(/figures/g);
var start;
var end;
for(arr = 0; arr <=ansa.length; arr++)
{
}
if(vendorLine)
{
vendorInfo = ansa.split(' ',ansa.replace('/Vendor|\r|\s/','') );
vendorFName = vendorInfo[0];
vendorLName = vendorInfo[1];
vendorID = vendorInfo[2];
vendor = {vendorid: vendorID, vendorInfo:{fname: vendorFName,lname:vendorLName} , orders:orders};
}
if( orderLine )
{
start = 1;
}
if( figuresLine )
{
end = 0;
}
if( !end || !start)
{
if( end < start )
{
ordersLine = explode(" ",v);
orderDesc = new Array();
//Remove the stuff we dont need. Like Qty ,Subtotal and Inv#. Once we remove them we reindex them
unset(ordersLine[0],ordersLine[2],ordersLine[count(ordersLine)]); // Removes Subtotal and Inv#
unset(ordersLine[count(ordersLine)]); // Removes QTY
ordersLine = array_values(ordersLine);
orderDate = ordersLine[0];
orderAmt = end(ordersLine);
/*foreach (ordersLine as key=>value)
{
if(key != 0)
{
if(key != count(ordersLine)-1)
{
array_push(orderDesc,value);
orderLines = array("vendorid"=>vendorID,"order"=>array("date"=> orderDate,"desc"=> implode(" " ,orderDesc), "amount"=>orderAmt) );
}
};
}*/
}
}
vendor['orders'] = orders;
saveFile(ansa);
}
function gettag() {
// gets the text following the / character or gets the param if it there
var tag='';
while(true) {
j++;
if (j>=len) break;
c = text.substr(j,1);
if (c==';') break;
if (c=='}') break;
if (c=="\\") {
j--;
break;
}
if (c=="{") {
//getpgraph();
break;
}
if (((c>='0')&&(c<='9'))||((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'))||c=="'"||c=="-"||c=="*" ){
tag= tag+c;
} else {
// end of tag
j--;
break;
}
}
return tag;
}
function getpgraph() {
// if the first char after a tag is { then throw out the entire paragraph
// this has to be nested
var nest = 0;
while(true) {
j++;
if (j>=len) break;
if (text.substr(j,1)=='}') {
if (nest==0) return;
nest--;
}
if (text.substr(j,1)=='{') {
nest++;
}
}
return;
}
我期望從這里創建一個干凈的格式良好的 JSON 文件,然后它可以在用戶所在的頁面上顯示回用戶。基本上用新上傳的解析數據刷新頁面。
有一個 JS RTF 解析器可能會有所幫助。 然后您需要對結果進行 json 編碼。 https://github.com/lazygyu/RTF-parser
不確定這是否能回答您的問題,但更多信息可能有助於鎖定修復程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.