私は xml ファイルに 2 つの要素を追加しようとしています。タグの直後にそれらを配置して、次のようにプロパティ タグ、新しいパラメーター、残りの rootnode の子を含む newchildren リストを作成します。
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>
しかし、 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)
誰でもこれで私を助けることができますか?