You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Thomas Thomas <de...@gmail.com> on 2006/11/27 13:31:08 UTC
Jakarta Configuration Question
Hi,
in an XML file, I have the following type of records :
<startlist>
<start parse="true" index="true">http://intranet.lu.anonyme1.bank</start>
<start parse="true" index="true">http://intranet.lu.anonyme2.bank</start>
</startlist>
I want to delete all the start elements, and add new start elements.
I try this :
FileConfiguration writer = new XMLConfiguration(path);
writer.setAutoSave(true);
writer.clearProperty("startlist.start");
But I have then the following :
<startlist>
<start index="true" parse="true"/>
<start index="true" parse="true"/>
</startlist>
As u can see the elemnt is still there, it just deleted the content.
How can I delete the elements totally?
Thank u.
Re: Jakarta Configuration Question
Posted by Oliver Heger <ol...@oliver-heger.de>.
Thomas Thomas wrote:
> Oliver,
> I could perfectly add elements and attributes how I want,
> But I have a problem that I can't solve with the doc.
>
> The "startlist" tag (included in the root tag) was below the proxy "tag",
> and when I do this :
>
> ((HierarchicalConfiguration)writer).clearTree("startlist");
>
> Iterator it = collection.getDirectoriesToParse();
> while (it.hasNext()) {
> writer.addProperty("startlist(-1).start(-1)",
> (String)it.next());
> writer.addProperty("startlist.start[@parse]", Boolean.TRUE);
> writer.addProperty("startlist.start[@index]", Boolean.TRUE);
> }
>
> It adds the startlist as last element of the root tag.
>
> It lost its position.
> Do u have any idea on how to fix this ?
>
> Thank u for any help.
>
Okay, this is a hard one.
I think, what you can do is to override the values of the start elements
instead of removing them and then adding them again. This can be done
with the setProperty() method. With
int cnt = ((HierarchicalConfiguration)
writer).getMaxIndex("startlist.start");
you can determine, how many <start> elements exist. Then in a loop you
can do:
for (int i = 0; it.hasNext() && i < cnt; i++) {
writer.setProperty("startlist.start(" + i + ")",
(String)it.next());
writer.setProperty("startlist.start(" + i + ")[@parse]",
Boolean.TRUE);
writer.setProperty("startlist.start(" + i + ")[@index]",
Boolean.TRUE);
}
If more elements have to be added then the file did contain before, the
remaining ones can be added in the usual way (as in your code above). If
some elements are no longer needed, they can be removed with clearTree()
specifying the exact index (as in the setProperty() calls above).
Hope that helps. The configuration API gives you only a limited control
over the order of the properties.
Oliver
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: Jakarta Configuration Question
Posted by Thomas Thomas <de...@gmail.com>.
Oliver,
I could perfectly add elements and attributes how I want,
But I have a problem that I can't solve with the doc.
The "startlist" tag (included in the root tag) was below the proxy "tag",
and when I do this :
((HierarchicalConfiguration)writer).clearTree("startlist");
Iterator it = collection.getDirectoriesToParse();
while (it.hasNext()) {
writer.addProperty("startlist(-1).start(-1)",
(String)it.next());
writer.addProperty("startlist.start[@parse]", Boolean.TRUE);
writer.addProperty("startlist.start[@index]", Boolean.TRUE);
}
It adds the startlist as last element of the root tag.
It lost its position.
Do u have any idea on how to fix this ?
Thank u for any help.
Re: Jakarta Configuration Question
Posted by Thomas Thomas <de...@gmail.com>.
Thank u Oliver,
just wanted to know if it was possible before to read the whole doc ;-)
I will carefully read the userguide.
Thank u for your precious support !
Re: Jakarta Configuration Question
Posted by Oliver Heger <ol...@oliver-heger.de>.
Thomas Thomas wrote:
> Thanks Oliver,
> I will try that,
> I need also to put attributes in an tag "start", which I must create.
> I need to add a property "start",
> but How do I create attributes with values in it ?
>
The syntax is something like
config.addProperty("startlist.start[@parse]", Boolean.TRUE);
Again the user guide contains detailed information.
Oliver
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: Jakarta Configuration Question
Posted by Thomas Thomas <de...@gmail.com>.
Thanks Oliver,
I will try that,
I need also to put attributes in an tag "start", which I must create.
I need to add a property "start",
but How do I create attributes with values in it ?
Re: Jakarta Configuration Question
Posted by Oliver Heger <ol...@oliver-heger.de>.
Thomas Thomas wrote:
> Thanks for the support Oliver,
> however, when I do clearTree(), the "startlist" tag disapear...
>
> I want that :
>
> <startlist>
> <start parse="true" index="true">http://intranet.lu.anonyme1.bank </start>
> <start parse="true" index="true">http://intranet.lu.anonyme2.bank</start>
> </startlist>
>
> To become that :
>
> <startlist>
> </startlist>
>
> Actually I do this because I need to put new "start" tags in the
> "startlist"
> tag ...
> Sounds very basic but I can't achieve it ...
>
Hm, interesting side effect of clearTree(). I guess that parent nodes,
which become empty by the clearTree() operation are automatically removed.
Whatever, is this really a problem that the <startlist> tag is removed?
You can still add new <start> tags by calling
addProperty("startlist.start", "a value");
Then the whole structure will be generated. More information about
adding properties to a hierarchical configuration can be found in the
user guide [1].
HTH
Oliver
[1]
http://jakarta.apache.org/commons/configuration/howto_xml.html#Adding_new_properties
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: Jakarta Configuration Question
Posted by Thomas Thomas <de...@gmail.com>.
Thanks for the support Oliver,
however, when I do clearTree(), the "startlist" tag disapear...
I want that :
<startlist>
<start parse="true" index="true">http://intranet.lu.anonyme1.bank </start>
<start parse="true" index="true">http://intranet.lu.anonyme2.bank</start>
</startlist>
To become that :
<startlist>
</startlist>
Actually I do this because I need to put new "start" tags in the "startlist"
tag ...
Sounds very basic but I can't achieve it ...
Re: Jakarta Configuration Question
Posted by Oliver Heger <ol...@oliver-heger.de>.
Thomas Thomas wrote:
> Hi,
>
> in an XML file, I have the following type of records :
>
> <startlist>
> <start parse="true" index="true">http://intranet.lu.anonyme1.bank</start>
> <start parse="true" index="true">http://intranet.lu.anonyme2.bank</start>
> </startlist>
>
> I want to delete all the start elements, and add new start elements.
> I try this :
>
>
> FileConfiguration writer = new XMLConfiguration(path);
> writer.setAutoSave(true);
>
> writer.clearProperty("startlist.start");
>
>
> But I have then the following :
>
> <startlist>
> <start index="true" parse="true"/>
> <start index="true" parse="true"/>
> </startlist>
>
> As u can see the elemnt is still there, it just deleted the content.
>
> How can I delete the elements totally?
> Thank u.
>
The clearProperty() method only removes the direct value of the
specified key, which in your case is the text value of the <start>
element; the attributes remain.
For removing complete sub trees (i.e. everything below the start
element, including its attributes and further sub elements)
HierarchicalConfiguration has the clearTree() method. If you cast your
configuration to a HierarchicalConfiguration object, you can try this
method.
HTH
Oliver
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org