You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ruben Silva <ru...@saphety.net> on 2007/07/25 13:43:05 UTC
[CONFIGURATION] addNodes()
Hi,
I'm trying to use the method XMLConfiguration.addNodes() to add a tree
of configuration properties to a node in other tree, like this:
public static void main(String ...args){
try{
configDestination = new XMLConfiguration("output.xml");
configSource = new XMLConfiguration("input.xml");
}
catch(ConfigurationException cex){
System.out.println("File not found");
}
Collection collection = new ArrayList();
collection = configSource.getRoot().getChildren();
configDestination.addNodes("newNodes", collection);
try {
configDestination.save();
} catch (ConfigurationException e) {
System.out.println("Error saving");
e.printStackTrace();
}
}
The XML files:
input.xml
<rootNode>
<newNodeChild>
<newNodeChildChild>child value</newNodeChildChild>
<newNodeChildChild>child value 2</newNodeChildChild>
<newNodeChildChild>child value 3</newNodeChildChild>
</newNodeChild>
</rootNode>
output.xml
<testRootNode>
<test>TEST</test>
</testRootNode>
output.xml after running the code:
<testRootNode>
<test>TEST</test>
<newNodes/>
</testRootNode>
Expected output.xml:
<testRootNode>
<test>TEST</test>
<newNodes>
<newNodeChild>
<newNodeChildChild>child value</newNodeChildChild>
<newNodeChildChild>child value 2</newNodeChildChild>
<newNodeChildChild>child value 3</newNodeChildChild>
</newNodeChild>
<newNodes/>
</testRootNode>
Hope someone can help me.
--
Ruben Silva
Research & Development – Saphety Level - Trusted Services SA
E-mail: ruben.silva@saphety.net
Re: [CONFIGURATION] addNodes()
Posted by Oliver Heger <ol...@oliver-heger.de>.
Ruben Silva wrote:
> Hi,
>
> I'm trying to use the method XMLConfiguration.addNodes() to add a tree
> of configuration properties to a node in other tree, like this:
>
> public static void main(String ...args){ try{
> configDestination = new XMLConfiguration("output.xml");
> configSource = new XMLConfiguration("input.xml");
> }
> catch(ConfigurationException cex){
> System.out.println("File not found");
> }
> Collection collection = new ArrayList();
> collection = configSource.getRoot().getChildren();
> configDestination.addNodes("newNodes", collection);
> try {
> configDestination.save();
> } catch (ConfigurationException e) {
> System.out.println("Error saving");
> e.printStackTrace();
> }
> }
>
>
> The XML files:
>
> input.xml
>
> <rootNode>
> <newNodeChild>
> <newNodeChildChild>child value</newNodeChildChild>
> <newNodeChildChild>child value 2</newNodeChildChild>
> <newNodeChildChild>child value 3</newNodeChildChild>
> </newNodeChild>
> </rootNode>
>
> output.xml
>
> <testRootNode>
> <test>TEST</test>
> </testRootNode>
>
> output.xml after running the code:
>
> <testRootNode>
> <test>TEST</test>
> <newNodes/>
> </testRootNode>
>
> Expected output.xml:
>
> <testRootNode>
> <test>TEST</test>
> <newNodes>
> <newNodeChild>
> <newNodeChildChild>child value</newNodeChildChild>
> <newNodeChildChild>child value 2</newNodeChildChild>
> <newNodeChildChild>child value 3</newNodeChildChild>
> </newNodeChild>
> <newNodes/>
> </testRootNode>
>
> Hope someone can help me.
>
I think you have spotted a bug. I did some testing and found out that
the addNodes() method in principle works correctly. However the copied
nodes still contain a reference to their old configuration (because you
directly fetched them from the root node of the source configuration).
Because of this reference they are not detected as new nodes when the
destination configuration is saved, and hence not written to disk.
I think addNodes() should reset this reference, so that the added nodes
can be detected as new nodes. (But then you have to be aware that you
break the source configuration because a node can only be contained in
exactly one configuration.)
Would you be so kind to open a new ticket in our bug tracking system [1]
for this issue?
Many thanks
Oliver
[1] http://jakarta.apache.org/commons/configuration/issue-tracking.html
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org