使用Javascript从XML文件中获取参数名称

时间:2017-05-25 15:26:18

标签: javascript xml dom jasper-reports

我正在尝试从Jasper Reports XML文件(我上传的)中提取参数。

这是我到目前为止所做的:

 var file = document.getElementById("fileToUpload").files[0];
            //You could insert a check here to ensure proper file type
            var reader = new FileReader();
            reader.readAsText(file);
            reader.onloadend = function(){

                var parser = new DOMParser();

                var doc = parser.parseFromString( reader.result, "application/xml");

                //doc.getElementsByTagName("parameter")[0]

                var parameters = doc.getElementsByTagName("parameter");

然后,我可以做parameters[0]并获得:

<parameter name=​"report_type_id" class=​"java.lang.Integer">​</parameter>​

我想要的是参数的名称。

这就是XML的样子(截断):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-05-25T10:35:12 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1fe7af1a-adc5-48b5-a571-37c521a3ccea">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="dev_selection"/>
    <parameter name="report_type_id" class="java.lang.Integer"/>
    <queryString language="SQL">
        <![CDATA[select * from reports where report_type_id = $P{report_type_id}]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="sql" class="java.lang.String"/>
    <field name="report_type_id" class="java.lang.String"/>
    <background>

米克

1 个答案:

答案 0 :(得分:1)

var file = document.getElementById("fileToUpload").files[0];
var reader = new FileReader();
reader.readAsText(file);
reader.onloadend = function () {

  var parser = new DOMParser();
  var doc = parser.parseFromString(reader.result, "application/xml");
  var params = doc.querySelectorAll('parameter');
  var names = [];
  for (var i = 0; i < params.length; i++) {
    names.push(params[i].getAttribute('name'));
  }

}