//ESPERO QUE TE SIRVA. UN SALUDO
import java.io.*;
import java.util.*;
import java.net.*;
import oracle.xml.parser.v2.*;
import org.w3c.dom.*;
import javax.swing.*;
public class Clase_examen
{
public Clase_examen()
{
try
{
general();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
public static void main(String[] args)
{
Clase_examen clase_examen = new Clase_examen();
}
public static void general()
{
String nom_fich=new String("C:\\ejemplosxml\\examen\\examen.xml");
XMLDocument doc;
XMLElement el;
doc=parsear(nom_fich);
generarnuevoxml(doc);
}
//PARSEAR
static XMLDocument parsear(String nom_fich)
{
XMLDocument doc=null;
try
{
DOMParser parser;
URL url;
parser=new DOMParser();
url=CrearURL(nom_fich);
parser.setErrorStream(System.err);
parser.setValidationMode(true);
parser.showWarnings(true);
parser.parse(url);
doc=parser.getDocument();
}
catch (Exception e)
{
// System.out.println(e.toString());
JOptionPane.showMessageDialog(new JFrame(),e.toString(), "ERROR en Parser!!!",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println("Parseado Documento: " + doc.getDocumentElement().getNodeName());
return doc;
}//fin parsear
//CREAR URL
static URL CrearURL(String fileName)
{
URL url = null;
try
{
url = new URL(fileName);
}
catch (MalformedURLException ex)
{
File f = new File(fileName);
try
{
StringBuffer buffer_path=new StringBuffer();
String path=new String();
boolean enc=false;
int i;
for (i=0;( (i<fileName.length()) && (enc==false) );i++)
{
if (fileName.charAt(i)=='\\')
enc=true;
}
if (enc==true)
{
buffer_path.append(fileName);
path=buffer_path.toString();
}
else
{
buffer_path.append(f.getAbsolutePath());
path=buffer_path.toString();
}
String fs = System.getProperty("file.separator");
if (fs.length() == 1)
{
char sep = fs.charAt(0);
if (sep != '/')
path = path.replace(sep, '/');
if (path.charAt(0) != '/')
path = '/' + path;
path = "file://" + path;
url = new URL(path);
}
}
catch (MalformedURLException e)
{
System.exit(0);
}
}
return url;
}
public static void generarnuevoxml(XMLDocument doc)
{
XMLDocument newdoc = new XMLDocument();
XMLElement nodotienda,modelo,venta, nodoventa, nodocliente, nodoauto, nodoprecio;
Attr atrauto;
NodeList nlModelos,nlVentas;
String marcamodelo;
int i,j;
try{
File fres=new File("C:\\ejemplosxml\\examen\\examen_resultDOM.xml");
FileOutputStream salida=new FileOutputStream(fres);
//creo la raiz del nuevo documento: tienda
nodotienda=(XMLElement)newdoc.createElement("tienda");
//voy recorriendo el documento viejo
nlModelos = doc.getElementsByTagName("modelo");
for (i=0; i<nlModelos.getLength();i++)
{
modelo = (XMLElement)nlModelos.item(i);
nlVentas = modelo.getElementsByTagName("venta");
for (j=0; j<nlVentas.getLength();j++)
{
venta = (XMLElement)nlVentas.item(j);
//Por cada venta tengo un nuevo elemento venta dentro de tienda:
nodoventa=(XMLElement)newdoc.createElement("venta");
//Creo el nodo cliente que irá dentro de venta
nodocliente = (XMLElement)newdoc.createElement("cliente");
nodocliente.addText(((XMLElement)venta.getElementsByTagName("cliente").item(0)).getText());
//añado el nodo cliente al nodo venta
nodoventa.appendChild((Node)nodocliente);
//Creo el nodo automovil que irá dentro de venta
nodoauto = (XMLElement)newdoc.createElement("automovil");
//en marcamodelo meto la marca y luego le concateno el modelo (para hacerlo por partes
//que se ve más sencillo)
marcamodelo = ((XMLElement)modelo.getElementsByTagName("marca").item(0)).getText();
marcamodelo = marcamodelo.concat(((XMLElement)modelo.getElementsByTagName("nombre").item(0)).getText());
nodoauto.addText(marcamodelo);
//al nodo automovil le añado combustible como atributo
nodoauto.setAttribute("combustible",modelo.getAttribute("combustible"));
//añado el nodo auto al nodo venta
nodoventa.appendChild((Node)nodoauto);
//creo el nodo preciobase dentro de venta
nodoprecio = (XMLElement)newdoc.createElement("preciobase");
nodoprecio.addText(((XMLElement)modelo.getElementsByTagName("precio").item(0)).getText());
//añado el nodo precio al nodo venta
nodoventa.appendChild((Node)nodoprecio);
//añado el nodo venta, ya completo, al nodo tienda
nodotienda.appendChild((Node)nodoventa);
}
}
newdoc.appendChild((Node)nodotienda);
newdoc.print(salida);
salida.close();
System.out.println("Documento generado");
}catch (Exception e)
{
JOptionPane.showMessageDialog(new JFrame(),e.toString(), "ERROR en Fichero!!!",JOptionPane.INFORMATION_MESSAGE);
}
}
}