XML Parcing With JDOM

2

Good I am trying to show the content of an XML through the JDOM library, but apparently it is not showing me the sub-nodes of a certain field of the XML file, I do not know if the solution I am trying to implement is quite new in this parceo of XML documents, I leave the code.

SAXBuilder builder = new SAXBuilder();
File xmlFile = new File( "E:\Desarollo\AED\workspace\Ficheros\libros.xml" );   
try
 {
       Document document = (Document) builder.build( xmlFile );
       Element rootNode = document.getRootElement();
        List list = rootNode.getChildren( "Libro" );


        for ( int i = 0; i < list.size(); i++ ) {

            Element Libros = (Element) list.get(i);

            String isbn = Libros.getAttributeValue("ISBN");
            String titulo = Libros.getAttributeValue("Titulo");
            System.out.println( "Titulo: " + titulo+" "+"ISBN= "+isbn );

            List<Element> lista_campos = Libros.getChildren();

            for ( int j = 0; j < lista_campos.size(); j++ ) {

                Element campo = (Element)lista_campos.get( j );

                String Ejemplar = campo.getChildTextTrim("Ejemplar");
                System.out.println(Ejemplar);

                String Autor = campo.getChildTextTrim("Autor");
                System.out.println(Autor);


            }
        }

And this is giving me the following console output

As you can see there are certain nodes that I put them to NULL my doubt is as follows as I get to print all the nodes not only the first, I leave you also the original XML file.

Any kind of help or advice to modify the code will be welcome thanks in advance community.

    
asked by Ricki 28.10.2016 в 01:34
source

1 answer

0

You're eating the Exemplary label so it does not work correctly because of that:

Element ejemplares = Libros.getChild("Ejemplares");
List<Element> listaEjemplares = ejemplares.getChildren();

for ( int j = 0; j < listaEjemplares.size(); j++ ) {
     Element campo = (Element)listaEjemplares.get( j );
     String Ejemplar = campo.getChildTextTrim("Ejemplar");
     System.out.println(Ejemplar);
}
Element autores = Libros.getChild("Autores");
List<Element> listaAutores = autores.getChildren();

for ( int j = 0; j < listaAutores.size(); j++ ) {
     Element campo = (Element)listaAutores.get( j );
     String Autor = campo.getChildTextTrim("Autor");
     System.out.println(Autor);
}
    
answered by 28.10.2016 / 01:54
source