Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [JAVA] Conversione di un ResultSet con Gerarchia in un XML

    Salve a tutti,

    Ho un problemuccio, devo creare da un resultSet gerarchico (in cui ho un campo che mi dice chi è il padre di un elemento) un xml ricorsivo.

    faccio un esempio:

    /00001 2 RIL
    /00001/00001 3 SRV
    /00001/00001/00001 4 CTX
    /00001/00002 3 RTG


    dove il primo /00001 (RIL) è la root che ha due fratelli /00001/00001 (SRV) e /00001/00002 (RTG) e /00001/00001 (SRV) ha un figlio /00001/00001/00001(CTX)

    ora da questo result set vorrei tirare fuori un xml che segua questa struttura gerarchica.

    Esistono delle librerie che in automatico facciano questo o devo crearmi io un algoritmo?

    grazie

  2. #2
    Ho usato questo codice (con sorgente un altro file xml invece di un resultSet) ma sbaglio qualcosa perché mi viene restituito solo il nodo radice:

    codice:
    public static boolean writeXml(Object o,Document out, int depth, Element root) {
    		String result = "";
    		Element item1 = new Element("temp");
    		stampaSpazi(depth);
    		if (o instanceof Element) {
    			Element element = (Element) o;
    			if (((Element) o).isRootElement()){
    				//System.out.println("Root Element:" + ((Element) o).getName());
    			//	result =  " "+result + " Root Element:" + ((Element) o).getName();
    				root.setName(((Element) o).getName());
    				
    			}	
    			else{
    				System.out.println("Element: " + element.getName());
    			//	result = " "+result+" Element: " + element.getName();
    				
    				item1=item1.setName(element.getName());
    				System.out.println("ELEMENTO: " + item1.getName());
    				
    				root=root.addContent(item1);
    
    				System.out.println("Grandezza lista: " + root.getChildren().size());
    				 
    			}
    			// Itera sugli eventuali attributi dell'elemento
    			if (!((Element) o).getAttributes().isEmpty()) {
    				Iterator iter = element.getAttributes().iterator();
    				while (iter.hasNext()) {
    					Object attribute = iter.next();
    					writeXml(attribute,out, depth+1, root);
    					
    				//	result = result+listNodes(attribute, depth + 1);
    
    				}
    			}
    			// Itera sui figli dell'elemento
    			List children = element.getContent();
    			Iterator iterator = children.iterator();
    			while (iterator.hasNext()) {
    				Object child = iterator.next();
    				//result = result + listNodes(child, depth + 1);
    		
    					writeXml(child,out, depth+1, root);
    		
    				
    			}
    		} else if (o instanceof Document) {
    
    			Document doc = (Document) o;
    			List children = doc.getContent();
    			Iterator iterator = children.iterator();
    			while (iterator.hasNext()) {
    				Object child = iterator.next();
    			//	result= result+ listNodes(child, depth + 1);
    				writeXml(child,out, depth+1, root);
    			}
    		} else if (o instanceof Comment) {
    			//result = " "+result+" Comment: " +((Comment) o).getText();
    			root.addContent(((Comment) o).getText());	
    //			System.out.println("Comment:");
    //			System.out.println(((Comment) o).getText());
    		} else if (o instanceof CDATA) {
    //			result = " "+result+" Sezione CDATA: " +((CDATA) o).getText();
    			root.addContent(((CDATA) o).getText());
    //			System.out.print("Sezione CDATA:");
    //			// CDATA è una sottoclasse di Text di conseguenza questo test
    //			// va prima di quello per Text.
    //			System.out.println(((CDATA) o).getText());
    		} else if (o instanceof Text) {
    			root.setText(((Text) o).getValue());
    //			result = " "+result+" Text: " +((Text) o).getValue();
    //			System.out.print("Text:");
    //			System.out.println(((Text) o).getValue());
    		} else if (o instanceof Attribute) {
    			root.setAttribute(((Attribute) o).getQualifiedName(),((Attribute) o).getValue());
    			//result = " "+result+" Attribute : " +((Attribute) o).getQualifiedName()+ " "+((Attribute) o).getValue();
    //			System.out.print("Attribute:");
    //			System.out.print(((Attribute) o).getQualifiedName());
    //			System.out.println(((Attribute) o).getValue());
    		} else {
    			//result = " "+result+" Tipo non previsto: " + o.getClass();
    //			System.out.println("Tipo non previsto: " + o.getClass());
    		}
    	
    		return true;
    	}
    Non riesco a capire cosa sbaglio..

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.