1

我正在尝试将两个元素添加到 xml 文件中,并且我想将它们放在标签之后,以便我创建一个包含属性标签的 newchildren 列表,然后是新参数,然后是其余的 rootnode 子节点,如下所示:

public void addParameters(Element rootNode) {

    Element resourcesParameter;
    Element subreportsParameter;

    List<Element> children;
    List<Element> newChildren;
    children = rootNode.getChildren("property", Namespace.getNamespace("http://jasperreports.sourceforge.net/jasperreports"));

    System.out.println("AAAAAAAAAAAAAAAAA " + Collections.indexOfSubList(rootNode.getChildren(), children));

    newChildren = rootNode.getChildren().subList(0, children.size());
    System.out.println(" size with property : " + newChildren.size() );

    resourcesParameter =  addParameter("RESOURCES_DIR", "resources");
    newChildren.add(resourcesParameter);
    System.out.println(" size with resources : " + newChildren.size() );


    subreportsParameter =  addParameter("SUBREPORT_DIR", "subreports" );
    newChildren.add(subreportsParameter);
    System.out.println(" size with subreports : " + newChildren.size() );
    for(int i =0; i< newChildren.size(); i++) {
        System.out.println("newchildren element " + i + " :" + newChildren.get(i) );
    }

    List<Element> rest = rootNode.getChildren().subList(children.size() + 2, rootNode.getChildren().size());
    System.out.println(" size rest : " + rest.size() );
    for(int i =0; i< rest.size(); i++) {
        System.out.println(" rest element " + i + " :" + rest.get(i) );
    }

    newChildren.addAll(rest);
    System.out.println("size newchildren : " + newChildren.size() );

    rootNode.getChildren().clear();
    rootNode.getChildren().addAll(newChildren);

}

addParameter 方法的代码:

public Element addParameter (String attributeName, String path) {
    Element parameter;
    Element defaultValueExpression;

    parameter = new Element("parameter", "http://jasperreports.sourceforge.net/jasperreports");
    parameter.setAttribute(new Attribute("name", attributeName));
    parameter.setAttribute(new Attribute("class", "java.lang.String"));
    parameter.setAttribute(new Attribute("isForPrompting", "false"));

    defaultValueExpression = new Element("defaultValueExpression", "http://jasperreports.sourceforge.net/jasperreports");
    defaultValueExpression.setContent(new CDATA('"'+ path + File.separator + '"'));

    parameter.addContent(defaultValueExpression);

    return parameter;

}

预期的 xml 文件应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<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="report name" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="384"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["subreports/"]]></defaultValueExpression>
</parameter>
<parameter name="RESOURCES_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["resources/"]]></defaultValueExpression>
</parameter>
<queryString language="xPath">
    <![CDATA[/pages/page]]>
</queryString>
<field name="noClt" class="java.lang.String">
    <fieldDescription><![CDATA[noClt]]></fieldDescription>
</field>
<field name="noFact" class="java.lang.String">
    <fieldDescription><![CDATA[noFact]]></fieldDescription>
</field>
<field name="sarlatLe" class="java.lang.String">
    <fieldDescription><![CDATA[sarlatLe]]></fieldDescription>
</field>
<field name="noPage" class="java.lang.String">
    <fieldDescription><![CDATA[noPage]]></fieldDescription>
</field>
<field name="adresseligne1" class="java.lang.String">
    <fieldDescription><![CDATA[groupeAdresse/ligne1]]></fieldDescription>
</field>
<field name="adresseligne2" class="java.lang.String">
    <fieldDescription><![CDATA[groupeAdresse/ligne2]]></fieldDescription>
</field>
<field name="adresseligne3" class="java.lang.String">
    <fieldDescription><![CDATA[groupeAdresse/ligne3]]></fieldDescription>
</field>
<field name="adresseligne4" class="java.lang.String">
    <fieldDescription><![CDATA[groupeAdresse/ligne4]]></fieldDescription>
</field>
<field name="adresseligne5" class="java.lang.String">
    <fieldDescription><![CDATA[groupeAdresse/ligne5]]></fieldDescription>
</field>
    .....
 </jasperReport>

但是在将 rest 添加到 newchildren List 时出现此错误:

Exception in thread "AWT-EventQueue-0" org.jdom2.IllegalAddException: The Content already has an existing parent "jasperReport"
at org.jdom2.ContentList.checkPreConditions(ContentList.java:211)
at org.jdom2.ContentList.add(ContentList.java:244)
at org.jdom2.ContentList$FilterList.addAll(ContentList.java:1067)
at java.util.SubList.addAll(AbstractList.java:684)
at java.util.SubList.addAll(AbstractList.java:674)
at fr.srd.remap.ui.gui.ProjectExporter.addParameters(ProjectExporter.java:387)
at fr.srd.remap.ui.gui.ProjectExporter.prepareProject(ProjectExporter.java:140)
at fr.srd.remap.ui.gui.ProjectExporter.editReportFiles(ProjectExporter.java:208)
at fr.srd.remap.ui.gui.ProjectExporter.editReportFiles(ProjectExporter.java:167)
at fr.srd.remap.ui.gui.ProjectExporter.editReportFiles(ProjectExporter.java:167)
at fr.srd.remap.ui.gui.ProjectExporter.editReportFiles(ProjectExporter.java:167)
at fr.srd.remap.ui.gui.ProjectExporter.prepareProject(ProjectExporter.java:132)
at fr.srd.remap.ui.gui.ProjectExporter.export(ProjectExporter.java:59)

谁能帮我这个 ?

4

1 回答 1

1

尝试这个 :

newChildren = new ArrayList<Element>(rootNode.getChildren().subList(0, children.size()));

于 2013-07-30T10:32:07.193 回答