Leer un fichero xml en un programa java

palomarp
25 de Julio del 2008
Hola,

me gustar铆a saber si hay alguna forma de obtener los datos de un xml en un programa java para poder trabajar despu茅s con esos datos.

Muchas gracias por vuestra ayuda.

Un saludo,

Paloma

carlos
25 de Julio del 2008
Esto te lo estoy copiando de un blog que encontr茅 con muchos ejemplos y c贸digo fuente, hace un monton de cosas ademas de leer un xml pero creo que te servir谩 de ejemplo, te paso la direcci贸n de donde lo saque por si quieres ver todo el fuente.
http://carrypotter.blogspot.com/2008/07/utilitario-leer-y-escribir-registros-en.html
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true); //VALIDAR CONTRA DTD
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(pathFormatXMLFile);


Element archivoNode= dom.getDocumentElement();
NodeList list= archivoNode.getElementsByTagName("registro");

for(int i=0; i<list.getLength() ; i++){
Node registro =list.item(i);

String type= registro.getAttributes().getNamedItem("type").getNodeValue();
String start=registro.getAttributes().getNamedItem("start").getNodeValue();
String key=registro.getAttributes().getNamedItem("key").getNodeValue();
String length=registro.getAttributes().getNamedItem("length").getNodeValue();

UyFileRow row = new UyFileRow();
row.setType(type);
row.setKey(key);
row.setStart(Integer.valueOf(start).intValue());
row.setLength(Integer.valueOf(length).intValue());

if(registro.getAttributes().getNamedItem("filler") != null){
row.setFiller(registro.getAttributes().getNamedItem("filler").getNodeValue() );
}

NodeList atributosList=registro.getChildNodes();
for(int j=0; j < atributosList.getLength();j++ ){
Node atributos= atributosList.item(j);
if(atributos.getNodeName().equals("atributos")){

NodeList aux= atributos.getChildNodes();
for(int k=0; k < aux.getLength(); k++){
Node atributo= aux.item(k);

if(atributo.getNodeName().equals("atributo")){
NamedNodeMap map= atributo.getAttributes();

String attrType=map.getNamedItem("type").getNodeValue();
String attrPropertyName=map.getNamedItem("propertyName").getNodeValue();
String attrStart =map.getNamedItem("start").getNodeValue();
String attrLength =map.getNamedItem("length").getNodeValue();

UyFileField field = new UyFileField();
field.setType(attrType);
field.setLength(Integer.valueOf(attrLength).intValue());
field.setStart(Integer.valueOf(attrStart).intValue());
field.setPropertyName(attrPropertyName);
row.getFields().add(field);

if(map.getNamedItem("pad") != null){
field.setPad(map.getNamedItem("pad").getNodeValue() );
}

if(map.getNamedItem("padLeft")!=null){
field.setPadLeft( Boolean.valueOf((map.getNamedItem("padLeft").getNodeValue())));
}
}
}
}

}

this.definedRows.put(row.getType(),row);

xela
25 de Julio del 2008
Si usas SAX estos post te pueden servir:

<a href=\\\"
http://www.latascadexela.es/2008/07/java-y-xml-sax.html\\\">Java y XML: SAX (I)</a>
<a href=\\\"
http://www.latascadexela.es/2008/07/java-y-xml-sax-ii.html\\\">Java y XML: SAX (I)</a>

xela
25 de Julio del 2008
Perd贸n, no salieron bien los enlaces:

Java y XML: SAX (I): http://www.latascadexela.es/2008/07/java-y-xml-sax.html
Java y XML: SAX (II): http://www.latascadexela.es/2008/07/java-y-xml-sax-ii.html

palomarp
25 de Julio del 2008
Hola,

much铆simas gracias por vuestra ayuda.

Un saludo,

Paloma