应使用XML结构显示文本区域行

时间:2014-12-15 04:31:54

标签: javascript jquery

我有一个小要求,我想读取文本区域行,需要像xml格式一样显示它。 我尝试使用以下代码,它成功地提供了xml结构的警报。 我们必须以xml格式显示整个行。这是我的代码

$(document).ready(function(){
    $("#saveImgID").click(function(){
              var xmlStart =  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Rowsets DateCreated=\"2014-11-20T16:41:17\" EndDate=\"2014-11-20T16:41:17\" StartDate=\"2014-11-20T16:41:17\" Version=\"14.0 SP4 Patch 0 (Nov 22, 2013)" +">";
              var xmlEnd = "</Rowset></Rowsets>";
        var lines = $('#fileContent1').val().split(/\n/);
     var texts = []
     for (var i=0; i < lines.length; i++) {
           if (/\S/.test(lines[i])) {
             texts.push($.trim(lines[i]));
          }
     }
     for (var i=0; i < texts.length; i++) {         
        alert(xmlStart+"<Rowset><Row><Line>"+texts[i]+"</Line></Row>"+xmlEnd);
      }
   });     
});

我必须显示

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Rowsets DateCreated="2014-11-20T16:41:17" EndDate="2014-11-20T16:41:17" StartDate="2014-11-20T16:41:17" Version="14.0 SP4 Patch 0 (Nov 22, 2013)>
    <Rowset>
    <Row>
        <Line>101,abc,andy,1000</Line>
    </Row>
    <Row>
        <Line>202,abc,sachin,2000</Line>
    </Row>
    <Row>
        <Line>303,abc,smith,3000</Line>
    </Row>
    </Rowset>
</Rowsets>

这是小提琴JsFiddle

3 个答案:

答案 0 :(得分:1)

只需在循环中构建XML并在其外部发出警报: http://jsfiddle.net/hy91m3tr/4/

$(document).ready(function(){
  $("#saveImgID").click(function(){
    var xmlStart =  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Rowsets DateCreated=\"2014-11-20T16:41:17\" EndDate=\"2014-11-20T16:41:17\" StartDate=\"2014-11-20T16:41:17\" Version=\"14.0 SP4 Patch 0 (Nov 22, 2013)" +">";
    var xmlEnd = "</Rowset></Rowsets>";
    var xml;
    var lines = $('#fileContent1').val().split(/\n/);
    var texts = []
    for (var i=0; i < lines.length; i++) {
      if (/\S/.test(lines[i])) {
        texts.push($.trim(lines[i]));
      }
    }
    xml = xmlStart + "<Rowset>";
    for (var i=0; i < texts.length; i++) {         
      xml += "<Row><Line>"+texts[i]+"</Line></Row>";
    }
    xml += xmlEnd;
    alert(xml);
  });     
});

答案 1 :(得分:1)

你真正需要的是在单个变量中构建它然后警告:尝试这样

var totalstr = xmlStart+"<Rowset>";
     for (var i=0; i < texts.length; i++) {         
        totalstr = totalstr +"<Row><Line>"+texts[i]+"</Line></Row>";
      }
     totalstr=totalstr+xmlEnd;
     alert(total);

答案 2 :(得分:1)

试试这个:

$(document).ready(function(){
    $("#saveImgID").click(function(){
        var xmlStart =  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Rowsets DateCreated=\"2014-11-20T16:41:17\" EndDate=\"2014-11-20T16:41:17\" StartDate=\"2014-11-20T16:41:17\" Version=\"14.0 SP4 Patch 0 (Nov 22, 2013)" +">";
        var xmlEnd = "</Rowset></Rowsets>";
        var lines = $('#fileContent1').val().split(/\n/);
        var texts = []
        for (var i=0; i < lines.length; i++) {
            if (/\S/.test(lines[i])) {
                texts.push($.trim(lines[i]));
            }
        }
        var wholeXML = xmlStart + "\n<Rowsets>\n\t<Rowset>";
        for (var i=0; i < texts.length; i++) {          
            wholeXML += "\n\t\t<Row>\n\t\t\t<Line>"+texts[i]+"</Line>\n\t\t</Row>";
        }
        wholeXML += xmlEnd;
        alert( wholeXML )
    });     
});

DEMO