/*==================================================
* Created on 17-ago-2004 by ghigueras
* LeerXML.java
====================================================*/
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.jdom.input.SAXBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class LeerXML {
public static void main(String[] args) {
try
{
/* ---------------------------------------------------------------------------------------------------------------------------*/
// DOMParser parser=(DOMParser)Class.forName("org.apache.xerces.parsers.DOMParser").newInstance();
// parser.parse("C:/My Documents/Programas/xml-xsl/xml.xml");
// Document dc=parser.getDocument();
/* --------------------------------------------------------------------------------------------------------------------------- */
// leerJDOM();
leerConXpath();
/* =================================
* ejecuta la funcion path para pruebas*/
// path();
System.exit(0);
/* ===================================*/
// confFromFile("OS");
// System.exit(0);
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//Document dc = docBuilder.parse(new File("C:/My Documents/Programas/xml-xsl/sim_jms.xml"));
Document dc = docBuilder.parse(new File("sim_jms.xml"));
// Se normaliza el texto del documento
dc.getDocumentElement().normalize();
/* */
Element elem=dc.getDocumentElement();
NodeList list=elem.getElementsByTagName("archivo");
System.out.println(list.getLength());
System.out.println(list.item(0).getChildNodes().item(1).getNodeName());
//System.out.println(list.item(0).getAttributes().getNamedItem("nom").getNodeValue());
String temp;
String file="rp48_GEN_20041014.1";
for (int i=0; i <>
{
// NodeList lsDatos=elem.getElementsByTagName("datos");
// System.out.println(lsDatos.getLength());
temp=list.item(i).getAttributes().getNamedItem("tipo").getNodeValue();
if (file.substring(0,file.indexOf("_")).equalsIgnoreCase(temp))
{
// tipo
System.out.println(list.item(i).getAttributes().getNamedItem("tipo").getNodeValue());
//topico
System.out.println("\t" + list.item(i).getChildNodes().item(1).getFirstChild().getNodeValue());
//mensaje
System.out.println("\t" + list.item(i).getChildNodes().item(3).getFirstChild().getNodeValue());
}
// else
// System.out.println(file.substring(0,file.indexOf("_")));
//// tipo
// System.out.println(list.item(i).getAttributes().getNamedItem("tipo").getNodeValue());
// //topico
// System.out.println("\t" + list.item(i).getChildNodes().item(1).getFirstChild().getNodeValue());
// //mensaje
// System.out.println("\t" + list.item(i).getChildNodes().item(3).getFirstChild().getNodeValue());
}
Node nodo=dc.getElementsByTagName("topico").item(1);
//System.out.println(nodo.getFirstChild().getNodeValue());
}
catch (Exception err) {
System.err.println("ERROR en el metodo main(args) de LeerXML.class");
err.printStackTrace();
}
}
/**
* @author ghigueras 17-nov-2004 17:43:51
*
*/
private static void confFromFile(String org) {
Vector vecDatos;
Vector vecCampos;
Vector vecRdo=new Vector();
try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document dc = docBuilder.parse(new File("pathConfig.xml"));
// Se normaliza el texto del documento
dc.getDocumentElement().normalize();
Element elem=dc.getDocumentElement();
// if (Boolean.valueOf(elem.getElementsByTagName("flag").item(0).getFirstChild().getNodeValue()) == Boolean.FALSE)
// {
// System.out.println("No hace nada....");
// return;
// }
NodeList list=elem.getElementsByTagName("datos_usuario");
for (int i=0; i
{
//inicializa vectores
vecDatos=new Vector();
vecCampos=new Vector();
//HashMap hp=new HashMap();
int cont=0;
//recorre todos los tags dentro de datos_usuario
for (int q=1;q
{
//System.out.println(q);
vecCampos.add(list.item(0).getChildNodes().item(q).getNodeName());
vecDatos.add(list.item(i).getChildNodes().item(q).getFirstChild().getNodeValue());
}
vecRdo.add(new IndraHashMap(vecDatos,vecCampos));
vecDatos=null;
vecCampos=null;
}
for (int v=0; v
{
IndraHashMap hp=(IndraHashMap)vecRdo.elementAt(v);
if (hp.valorDeCampoNull("TIPOORG").equals(org))
{
System.out.println(hp.valorDeCampoNull("TIPOORG"));
System.out.println(hp.valorDeCampoNull("A_TIPO"));
System.out.println(hp.valorDeCampoNull("USUARIO"));
System.out.println(hp.valorDeCampoNull("PASSWD"));
System.out.println(hp.valorDeCampoNull("RUTA_LOG"));
System.out.println(hp.valorDeCampoNull("DIR_SUBIR"));
System.out.println(hp.valorDeCampoNull("IP"));
System.out.println(hp.valorDeCampoNull("IP_SUBIDA"));
System.out.println(hp.valorDeCampoNull("PRIORIDAD"));
System.out.println(hp.valorDeCampoNull("REITENTOS"));
System.out.println(hp.valorDeCampoNull("ESPERAS"));
System.out.println(hp.valorDeCampoNull("IPS"));
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
else
System.out.println(hp.valorDeCampoNull("TIPOORG"));
}
}
catch (Exception err) {
System.err.println("ERROR en metodo confFromFile() de LeerXML.class");
err.printStackTrace();
}
}
/**
* @author ghigueras 18-oct-2004 21:56:10
*
*/
private static void path() {
String sql;
Vector vecDatos;
Vector vecCampos;
try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//Document dc = docBuilder.parse(new File("C:/My Documents/Programas/xml-xsl/sim_jms.xml"));
Document dc = docBuilder.parse(new File("pathConfig.xml"));
// Se normaliza el texto del documento
dc.getDocumentElement().normalize();
Element elem=dc.getDocumentElement();
if (Boolean.valueOf(elem.getElementsByTagName("flag").item(0).getFirstChild().getNodeValue()) == Boolean.FALSE)
{
System.out.println("No hace nada....");
return;
}
NodeList list=elem.getElementsByTagName("datos_usuario");
// sql="DELETE FROM TB_ORG_USUARIO";
// System.out.println(sql);
//System.out.println(list.item(0).getChildNodes().getLength());
//recorre todos los tabs datos_usuario
for (int i=0; i
{
//inicializa vectores
vecDatos=new Vector();
vecCampos=new Vector();
//HashMap hp=new HashMap();
int cont=0;
//recorre todos los tags dentro de datos_usuario
for (int q=1;q
{
//System.out.println(q);
vecCampos.add(list.item(0).getChildNodes().item(q).getNodeName());
vecDatos.add(list.item(i).getChildNodes().item(q).getFirstChild().getNodeValue());
}
Vector vecRdo=new Vector();
vecRdo.add(new IndraHashMap(vecDatos,vecCampos));
for (int v=0; v
{
IndraHashMap hp=(IndraHashMap)vecRdo.elementAt(v);
System.out.println(hp.valorDeCampoNull("IP"));
}
sql="UPDATE TB_ORG_USUARIO SET " + vecCampos.elementAt(2) + "='" + vecDatos.elementAt(2) + "'," +
vecCampos.elementAt(3) + "='" + vecDatos.elementAt(3) + "'," +
vecCampos.elementAt(4) + "='" + vecDatos.elementAt(4) + "'," +
vecCampos.elementAt(5) + "='" + vecDatos.elementAt(5) + "'," +
vecCampos.elementAt(6) + "='" + vecDatos.elementAt(6) + "' WHERE " +
vecCampos.elementAt(0) + "='" + vecDatos.elementAt(0) + "' AND " + vecCampos.elementAt(1) + "='" + vecDatos.elementAt(1) + "'" ;
System.out.println(sql + ";");
vecDatos=null;
vecCampos=null;
}
list=elem.getElementsByTagName("datos_path");
for (int i=0;i
{
vecDatos=new Vector();
vecCampos=new Vector();
for (int q=1; q
{
vecCampos.add(list.item(0).getChildNodes().item(q).getNodeName());
vecDatos.add(list.item(i).getChildNodes().item(q).getFirstChild().getNodeValue());
}
// vecCampos.add(list.item(0).getChildNodes().item(1).getNodeName());
// vecDatos.add(list.item(i).getChildNodes().item(1).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(3).getNodeName());
// vecDatos.add(list.item(i).getChildNodes().item(3).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(5).getNodeName());
// vecDatos.add( list.item(i).getChildNodes().item(5).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(7).getNodeName());
// vecDatos.add( list.item(i).getChildNodes().item(7).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(9).getNodeName());
// vecDatos.add( list.item(i).getChildNodes().item(9).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(11).getNodeName());
// vecDatos.add( list.item(i).getChildNodes().item(11).getFirstChild().getNodeValue());
sql="INSERT INTO TB_ORG_PATH (" + vecCampos.elementAt(0) + ", " + vecCampos.elementAt(1) + ", " +
vecCampos.elementAt(2) + ", " + vecCampos.elementAt(3) + ", " + vecCampos.elementAt(4) + ", " + vecCampos.elementAt(5) +
") VALUES ('" + vecDatos.elementAt(0) + "','" + vecDatos.elementAt(1) + "','" +
vecDatos.elementAt(2) + "','" + vecDatos.elementAt(3) + "','" + vecDatos.elementAt(4) + "','" +
vecDatos.elementAt(5) + "')";
System.out.println(sql + ";" );
vecDatos=null;
vecCampos=null;
}
list=elem.getElementsByTagName("datos_dir");
for (int i = 0; i <>
{
vecDatos=new Vector();
vecCampos=new Vector();
for (int q=1; q
{
vecCampos.add(list.item(0).getChildNodes().item(q).getNodeName());
vecDatos.add(list.item(i).getChildNodes().item(q).getFirstChild().getNodeValue());
}
// vecCampos.add(list.item(0).getChildNodes().item(1).getNodeName());
// vecDatos.add(list.item(i).getChildNodes().item(1).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(3).getNodeName());
// vecDatos.add(list.item(i).getChildNodes().item(3).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(5).getNodeName());
// vecDatos.add(list.item(i).getChildNodes().item(5).getFirstChild().getNodeValue());
//
// vecCampos.add(list.item(0).getChildNodes().item(7).getNodeName());
// vecDatos.add(list.item(i).getChildNodes().item(7).getFirstChild().getNodeValue());
sql="UPDATE TB_ORG_DIR SET " + vecCampos.elementAt(3) + "='" + vecDatos.elementAt(3) +"' WHERE " +
vecCampos.elementAt(0) + "='" + vecDatos.elementAt(0) + "' AND " + vecCampos.elementAt(1) + "='" + vecDatos.elementAt(1) +
"' AND " + vecCampos.elementAt(2) + "='" + vecDatos.elementAt(2) + "'";
System.out.println(sql + ";");
vecDatos=null;
vecCampos=null;
}
}
catch (Exception err) {
System.err.println("ERROR en metodo path() de LeerXML.class");
err.printStackTrace();
}
}
/**
*@author ghigueras 21-oct-2005 11:55:24
*/
private static void leerJDOM(){
try
{
SAXBuilder builder=new SAXBuilder();
org.jdom.Document doc= builder.build(new File("config.xml"));
org.jdom.Element ele= doc.getRootElement();
List ls=ele.getChildren();
Iterator it=ls.iterator();
while (it.hasNext() == true)
{
org.jdom.Element el=(org.jdom.Element)it.next();
System.out.println(el.getText());
}
org.jdom.Element el=ele.getChild("fusion");
ls=el.getChildren("archivo");
it=null;
it=ls.iterator();
while (it.hasNext() == true)
{
System.out.println(el.getChild("archivo").getAttribute("numero").getValue());
org.jdom.Element xx=(org.jdom.Element)it.next();
System.out.println(xx.getChild("tipo").getText()+ " " + xx.getChild("dir").getText());
}
System.out.println(ele.getChild("dsn").getText());
}
catch (Throwable err) {
System.out.println("ERROR en metod leerJDOM() de LeerXML.java");
err.printStackTrace();
}
}
/**java 1.5
*@author ghigueras 26/04/2008 13:27:20
* @throws XPathExpressionException
*/
private static void leerConXpath() {
try
{
//crea el documento
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=builder.parse(new File("config.xml"));
//evalua una expresion en todo el documento
XPath path=XPathFactory.newInstance().newXPath();
String expresoin="/conf/fusion/archivo";
Object nod= path.evaluate(expresoin, doc,XPathConstants.NODESET);
NodeList ls=(NodeList)nod;
System.out.println(ls.getLength());
for (int i=0;i
{
System.out.println( ls.item(i).getAttributes().getNamedItem("numero").getTextContent());
NodeList no=ls.item(i).getChildNodes();
expresoin="tipo";
Node nodo= (Node) path.evaluate(expresoin, no,XPathConstants.NODE);
System.out.println( nodo.getTextContent());
expresoin="dir";
nodo= (Node) path.evaluate(expresoin, no,XPathConstants.NODE);
System.out.println( nodo.getTextContent());
}
expresoin="/conf/dsn";
Node dsn= (Node) path.evaluate(expresoin, doc,XPathConstants.NODE);
System.out.println( dsn.getTextContent());
expresoin="//archivo[@numero=4]";
dsn= (Node) path.evaluate(expresoin, doc,XPathConstants.NODE);
ls=(NodeList)dsn;
for (int e=0;e
{
System.out.println(ls.item(e).getAttributes().getNamedItem("numero").getTextContent());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}