RE: no soy capaz de analizar un xml sencillisimo. me podeis ayudar?

"gabi_out" <[email protected]>
Newsgroups gmane.comp.java.javaspain
Message-ID <[email protected]>
Hola, te recomiendo que uses JDOM, es lo más fácil de usar que te
puedes imaginar, y lo más parecido a la metodología Java:

http://es.wikipedia.org/wiki/JDOM

En tu caso... sencillo

Lees el XML y lo guardas en un  org.jdom.Document

"Document xmlDoc"


Obtienes la raiz:

org.jdom.Element raiz = xmlDoc.getRootElement();


Obtienes sus hijos:

List<Element> childs = root.getChildren();


Y en esta lista, ya tendrias tus nodos  REGISTRO

a los que puedes ir accediendo con un Iterator y obteniendo los datos



Tambien podrías usar XPath
http://www.w3schools.com/Xpath/


http://www.jdom.org/docs/apidocs/org/jdom/xpath/XPath.html

XPath select = XPath.newInstance("//REGISTRO");  (dame todos los nodos
REGISTRO)
List<Element> lista = select.selectNodes(xmlDoc); (el documento de antes)


Espero que te sirva. Saludos


--- En [email protected], koxkorrita@... escribió:
>
> Hola
> tengo unos xmls sencillisimos del tipo:
> <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
> <REGISTROS>
> <REGISTRO>
> <ID>1</ID>
> <APLIC>primera</APLIC>
> </REGISTRO>
> <REGISTRO>
> <ID>2</ID>
> <APLIC>segunda</APLIC>
> </REGISTRO>
> </REGISTROS>Estoy intentando analziarlo y doy pasos pero no consigo
"en la
> misma vuenta" tomar lo que hay en cada nodo REGISTRO.
> es decir, hago lo sigueinte:
> 
> NodeList nl = doc.getDocumentElement().getElementsByTagName("APLIC");
> if (nl != null && nl.getLength() > 0) {
> for (int i=0; i < nl.getLength(); i++) {
> Element e = (Element)nl.item(i);
> if (e.getFirstChild().getNodeValue()!= null) {
> System.out.println("//------>: " + e.getFirstChild().getNodeValue());
> }
> }
> }
> 
> Con esto imprimo el valor de APLIC pero al lado, en la misma vuenta,
> quisiera tomar el valor del campo ID cosa que no peudo o no logro.
> Mi intencion es por cada nodo REGISTRO llenar un objeto (bean) que tiene
> dos propiedades, id y aplic par air añadiendolo a aun arraylist en
el cual
> van estos objetos.
> 
> Me podeis ayudar por favor?
> 
> gracias
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.