How to read objects from an XML?

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!

    
asked by anonymous 09.11.2016 / 22:58

1 answer

2

The Xml that was made available had errors, it was missing to 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();
}

    
10.11.2016 / 00:17