Recover the value of an attribute of a generic object in Java

Asked

Viewed 763 times

2

I’m making a system where I read Java objects of different types of XML files. So I don’t have a single object type and consequently I don’t know its attributes or methods. I would like to perform a search, in which I entered a string indicating attribute and value, and this search returned to me all objects that had this attribute and that was equal to the value in a ArrayList.

Example: I have some XML files, of the types Produto, Pessoa and Carro, are already read and instantiated. I want to look for objects that have the following feature ano: 1996, the classes Produto and Carro has the attribute ano, however Pessoa he doesn’t have it. It would traverse the objects, where it would be analyzed if the value is equal and would take the necessary steps, if the attribute did not exist it would return null or a Exception to be treated.

Someone has an idea of how I can do this recovery, the biggest problem is who can be objects of different types, is that a non-relational bank job, if it were relational would be very easy, with defined types.

I’d like something that as a .getAtributte (String nomeAtributo, Object o), where the former is an attribute name and the latter the object analyzed.

2 answers

1


From a single entity, for example Entidade, you will need to have inside her a Map<String, Object> mapa.

To each value obtained from xml, vc will assign to the object used mapa.put(colunaXml, valorColunaXml).

His method getAttribute(String coluna) should follow the pattern

for (Map.Entry<String, Object> entry : mapa.entrySet()) {
    if(entry.getKey().equals(coluna.toString()){
        return entry.getValue();
    }
}
  • Good idea, I’ll try :) !

  • Your answer together with this solved my problems: http://stackoverflow.com/questions/2989560/how-to-get-the-fields-in-an-object-via-reflection

0

Create an Objetoxml class that will be common to all of them.

Within Objetoxml, you can place a dynamic structure as a Chained List and when creating the object, you populate the list with the objects you received from the database.

In Objetoxml, you then create the method: o. getAtributte (String names tribute)

Which will basically go through the list of attributes and answer the value if it exists, or null if the attribute does not exist in the list.

  • Thanks for the answer but I think you do not understand, the types of objects can be any, I will use the system I am doing as a mini-database for a larger application, it will provide the classes, however it is not possible to determine which gets I will use, in Java is not as in PHP, as object.attribute()

  • I edited the answer to what seemed to be the question, rereading it. See if it helps.

  • Chained list, or a map? For me a map would be better in the situation.

  • And also if an Objetoxml is created for each xml entity, there is no need for the second parameter in the method.

  • It depends on the possibility of XML having repeated attributes, but you’re right, probably a map would suit.

  • But I will not know the entities in advance, precisely I am having problems in the creation of this getAtributte(), in coding whether it with a list or map this part is indifferent, the problematic question is to verify the existence of the attribute, identify the type of the attribute and consequently recover that value

  • If Voce already has the part that will read the xml file, for each item read, insert in the entity map with the key being the "column" of xml, and its respective value.

  • um... you don’t know anything about the database? is it possible to touch it to have a header or something you always know? Type, you don’t know where an attribute starts and where the value is?

  • 1

    Working with a fixed structure of a relational database is simple to know where everything is, the problem is when working with a non-relational database, in which data from the same collection has different attributes, such as in a document-oriented database.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.