Buscar este blog

jueves, 19 de agosto de 2010

Convertir a XML un String

El xml es el siguiente:



35
mmmm>
CJMC


35
mmm>
mmm



y las funciones de JDOM utilizadas son las siguientes:

public static void XMLParser(String xml) throws IOException{
Document doc = readDocument(xml);
List listaConten = doc.getContent();
Element elemto=(Element) listaConten.get(1);
List hijosRaiz = elemto.getChildren();
for ( Element hijo: hijosRaiz){
String nombre = hijo.getName();
String texto = hijo.getValue();
Element hij2=hijo.getChild("id");
System.out.println("\nEtiqueta: "+ hij2.getName()+". Texto: "+ hij2.getValue());


System.out.println("\nEtiqueta: "+nombre+". Texto: "+texto);
hijo.getAttributeValue("id");
// Obtenemos el atributo id si lo hubiera
String id = hijo.getAttributeValue("id");
}
String p="";
p="asd";
}

public static Document readDocument(String xml) throws IOException {
try {
SAXBuilder builder = new SAXBuilder();
org.jdom.Document result = builder.build(new
StringReader(xml));

return (Document) result;
} catch(JDOMException e) {
e.printStackTrace();
} catch(NullPointerException e) {
e.printStackTrace();
}
return null;
} //readDocument


A tener en cuenta CDATA para meter en una cadena texto xml. Carlos Javier Martín Cano