How to read objects from an XML?

Asked

Viewed 120 times

2

I have an XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_101" class="java.beans.XMLDecoder">
 <object class="java.util.ArrayList">
  <void method="add">
  <object class="modelo.Aluno">
<void property="cpf">
 <int>321</int>
</void>
<void property="data_nasc">
 <string>123</string>
</void>
<void property="email">
 <string>aluno@aluno</string>
</void>
<void property="matricula">
 <int>1</int>
</void>
<void property="nome">
 <string>aluno 1</string>
</void>
</object>
</void>
 <void method="add">
 <object class="modelo.Aluno">
<void property="cpf">
 <int>321</int>
</void>
<void property="data_nasc">
 <string>123</string>
</void>
<void property="email">
 <string>aluno@aluno</string>
</void>
<void property="matricula">
 <int>1</int>
</void>
<void property="nome">
 <string>aluno 2</string>
   </void>
 </object>
</void>
<void method="add">
 <object class="modelo.Aluno">
<void property="cpf">
 <int>321</int>
</void>
<void property="data_nasc">
 <string>123</string>
</void>
<void property="email">
 <string>aluno@aluno</string>
</void>
<void property="matricula">
 <int>1</int>
</void>
<void property="nome">
 <string>aluno 3</string>
</void>

I’m trying to read the objects as follows:

public /*ArrayList<Aluno>*/ void carregaAlunos()
{

    Aluno[] alunos = new Aluno[3];
    System.out.println("eeeeeee");
    try{
        XMLDecoder decoder = null;
        try{
            decoder = new XMLDecoder(
                    new FileInputStream("alunos.xml"));

            alunos = (Aluno[]) decoder.readObject();//ESSA LINHA NÃO ESTÁ FUNCIONANDO
            System.out.println("Alunos lidos: " + alunos.length);
            for (Aluno a : alunos)
                System.out.println(a.getNome());
        } finally {
            if (decoder != null)
                decoder.close();
        }
    } catch (IOException e){
        System.out.println(e.getMessage());
    }
}

How to proceed?

Thanks in advance!

  • Any error message appears?

1 answer

2

The Xml that was made available had errors, was missing close some items and this Xml returns a ArrayList and not a array simple:

<java version="1.8.0_101" class="java.beans.XMLDecoder">

XML:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_101" class="java.beans.XMLDecoder">
<object class="java.util.ArrayList">
    <void method="add">
        <object class="modelo.Aluno">
            <void property="cpf">
             <int>321</int>
            </void>
            <void property="data_nasc">
             <string>123</string>
            </void>
            <void property="email">
             <string>aluno@aluno</string>
            </void>
            <void property="matricula">
             <int>1</int>
            </void>
            <void property="nome">
             <string>aluno 1</string>
            </void>
        </object>
    </void>
    <void method="add">
        <object class="modelo.Aluno">
            <void property="cpf">
            <int>321</int>
        </void>
        <void property="data_nasc">
         <string>123</string>
        </void>
        <void property="email">
         <string>aluno@aluno</string>
        </void>
        <void property="matricula">
         <int>1</int>
        </void>
        <void property="nome">
        <string>aluno 2</string>
           </void>
        </object>
    </void>
    <void method="add">
        <object class="modelo.Aluno">
            <void property="cpf">
             <int>321</int>
            </void>
            <void property="data_nasc">
             <string>123</string>
            </void>
            <void property="email">
             <string>aluno@aluno</string>
            </void>
            <void property="matricula">
             <int>1</int>
            </void>
            <void property="nome">
             <string>aluno 3</string>
            </void>
        </object>
    </void>
</object>
</java>

Code:

ArrayList<Aluno> alunos;
XMLDecoder decoder = null;
try{
    decoder = new XMLDecoder(new FileInputStream("alunos.xml"));
    Object obj = decoder.readObject();
    alunos = (ArrayList<Aluno>) obj;
    System.out.println("Alunos lidos: " + alunos.size());
    for (Aluno a : alunos)
        System.out.println(a.getNome());
} finally {
    if (decoder != null)
        decoder.close();
}

Observing: a line has been added to the readObject() was giving error in conversion, first need to be passed to a Object then the cast for ArrayList, when you pass the doubt please provide the full Xml, what is in the question was missing close the keys.

Browser other questions tagged

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