parse string xml

0

I have the following XML code in a string what I want to do is a parse to print the information but I have not been able to with the following .jar xerces, jdom and jaxp does anyone know of another? or what versions of these might work all throw me error.

<?xml version = "1.0" encoding= "UTF-8"?> 
<!DOCTYPE list 
><list>
  <ListaMaterias>
    <codigoMateria>3525</codigoMateria>
    <descMateria>Aprender Biologia</descMateria>
    <id>1</id>
    <nombreMateria>Biologia</nombreMateria>
  </ListaMaterias>
  <ListaMaterias>
    <codigoMateria>3678</codigoMateria>
    <descMateria>Aprender Quimica</descMateria>
    <id>2</id>
    <nombreMateria>Quimica</nombreMateria>
  </ListaMaterias>
</list>

I'm trying with JDOM but when I want to print it prints me blank. It does not print anything just 3 blank spaces.

    public static void main(String[] args) throws IOException {

    List resultado = null;
    resultado = new ArrayList<>();
    resultado = listarDatos();
    XStream xstream = new XStream();
    String xml = xstream.toXML(resultado);

    String adicionar = "<?xml version = \"1.0\" encoding= \"UTF-8\"?> \n";
    String doctype = "<!DOCTYPE list \n>";
    String xml_m = adicionar + doctype + xml.replace("<newwebservicematerias.Materia>", "<ListaMaterias>").replace("</newwebservicematerias.Materia>", "</ListaMaterias>");
    //System.out.println(xml_m);

    org.jdom.input.SAXBuilder saxBuilder = new SAXBuilder();
    try {
        org.jdom.Document doc = saxBuilder.build(new StringReader(xml_m));
        String message = doc.getRootElement().getText();
        System.out.println(message);
    } catch (JDOMException e) {
// handle JDOMException
    } catch (IOException e) {
// handle IOException
    }

}

and tried with

        org.jdom.input.SAXBuilder saxBuilder = new SAXBuilder();
        try {
            org.jdom.Document doc = saxBuilder.build(new StringReader(xml_m));
            List cadena = doc.getContent();
            for (Object iterator : cadena) {
                System.out.println(iterator);
            }

        } catch (JDOMException e) {
// handle JDOMException
        } catch (IOException e) {
// handle IOException
        }

but the only thing that prints is:

[DocType: <!DOCTYPE list>]
[Element: <list/>]
    
asked by juancamilovallejos0 19.04.2018 в 05:18
source

2 answers

1

According to the JDOM documentation v1.1.3:

  

getRootElement ()
            -This will return the root Element for this Document

You should use the getContent (), which according to the documentation returns a List with the content of the document.

 try {
    org.jdom.Document doc = saxBuilder.build(new StringReader(xml_m));
    resultado = doc.getContent();
    for(List iterator: resultado){
       System.out.println(iterator);
    }
} catch (JDOMException e) {
    
answered by 19.04.2018 в 10:24
0

I think you've already turned around: either you use sax or you use xstream. Xstream is more powerful but it is not magic and you lack many configuration classes. I invite you to review my answer in this thread For example you need the java classes that represent the xml, you are missing the converter as in my example StringConverter and you lack the configuration driver as in my example XstreamTransformacion :

package ejemplo.xstream;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class StringConverter implements SingleValueConverter {
    @Override
    public boolean canConvert(Class arg0) {
        return arg0.equals(String.class);
    }
    @Override
    public Object fromString(String arg0) {
        return arg0;
    }
    @Override
    public String toString(Object arg0) {
        return (String) arg0;
    }

}
package ejemplo.xstream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XstreamTransformacion {
    private static XStream configuracion(){
        XStream xstream=new XStream(new DomDriver());
        xstream.alias("biblioteca", Biblioteca.class);
        xstream.alias("libro", Libro.class);
        xstream.addImplicitCollection(Biblioteca.class, "libros");
        xstream.useAttributeFor(Libro.class, "tipo");
        xstream.registerConverter(new StringConverter());   
        return xstream;
    }
    public static String toXML(Biblioteca biblioteca){
        XStream xstream=XstreamTransformacion.configuracion();
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+xstream.toXML(biblioteca);
    }             
    public static Biblioteca fromXML(String xml){
        XStream xstream=XstreamTransformacion.configuracion();
        return (Biblioteca)xstream.fromXML(xml);
    }
}

Another thing that I think is causing you big problems is your XML. <!DOCTYPE list> It is used in definitions in DTDs or XSDs but you are in XML, so here it does not go and I think that is making you a lot of noise.

    
answered by 19.04.2018 в 18:04