How to store and retrieve objects using XML?

2019-09-02 15:45发布

问题:

I have a class called Word with these four attributes: text, caseSensitive, matchSimilar and relevance.

I have 10 categories for these words: (allowDetermining,allowHigh,...,denyDetermining,denyHigh,etc.).

I want to be able to store new words on each categories or retrieve all words from some category. How can I do this.

I think the XML structure should be like this, but I don't know if I'm right:

<allowDetermining>
    <word>
        <text>Renato</text>
        <caseSensitive>true</caseSensitive>
        <matchSimilar>false</matchSimilar>
        <relevance>0.75</relevance>
     </word>
</allowDetermining>
<allowHigh>
    ...
</allowHigh>
...

At the moment, I'm using the XMLConfiguration class from Apache Commons Configuration. Any idea of how do this using it? And with another library?

回答1:

Is use of XML a requirement, or just your first idea? For persisting configuration data or working sets, JSON (data) or YAML (configuration) might make more sense and be bit easier to do as well. XML works better for its original use case (text markup), but for data-oriented stuff it is quickly falling out of favor, and for good reason.