Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2944 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  xml, dom, java

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

xml, dom, java

n°278849
Shogun2002
Posté le 03-01-2003 à 16:54:54  profilanswer
 

Voila en faisant une petite recherche sur le forum, g trouvé un lien sur le site d'IBM qui explique comment extraire des données dans une BD et ensuite le mettre dans un fichier XML.
 
Mais ça marche pas  :sweat:  
 
Je voudrais savoir d'où viens le pb ...
Car c pas ma bd car il extrait bien les données ...
Il fait un "Java.lang.NullPointerException at xml.extract.main(extract.java:143)"
 
 
Voici le fichier
:
..
            //Get the original values based on the mapping information
            Element oldValueElement = (Element)thisRow.getElementsByTagName(oldField).item(0);
            String oldValue = oldValueElement.getFirstChild().getNodeValue();// <=ET LA C LE DRAME !
            //Create the new element
            Element newElement = newDoc.createElement(newElementName);
            newElement.appendChild(newDoc.createTextNode(oldValue));
            //Retrieve list of new elements
            NodeList newAttributes = thisElement.getElementsByTagName("attribute" );
            System.out.println("Test10" );
            for (int k=0; k < newAttributes.getLength(); k++) {
               //For each new attribute
               //Get the mapping information
               Element thisAttribute = (Element)newAttributes.item(k);
               String oldAttributeField = thisAttribute.getFirstChild().getNodeValue();
               String newAttributeName = thisAttribute.getAttribute("name" );
               //Get the original value
               oldValueElement = (Element)thisRow.getElementsByTagName(oldAttributeField).item(0);
               String oldAttributeValue = oldValueElement.getFirstChild().getNodeValue();
               //Create the new attribute
               newElement.setAttribute(newAttributeName, oldAttributeValue);
            }
            System.out.println("Test11" );
            //Add the new element to the new row
            newRow.appendChild(newElement);
         }
         //Add the new row to the root
         newRootElement.appendChild(newRow);
      }
      //Add the new root to the document
      newDoc.appendChild(newRootElement);
   }
}
 

mood
Publicité
Posté le 03-01-2003 à 16:54:54  profilanswer
 

n°278850
Shogun2002
Posté le 03-01-2003 à 16:56:05  profilanswer
 

g voulu poster mon fichier en entier mais le forum fait un bug ?!?

n°278851
Shogun2002
Posté le 03-01-2003 à 16:57:07  profilanswer
 

package xml;
import connexion.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
 
public class extract extends Object {
  static Connection bdc;
 
   public static void main (String args[]){
 
      //Create the Document object
      Document mapDoc = null;
      //Define a new Document object
      Document dataDoc = null;
      //Create the new Document
      Document newDoc = null;
 
      try {
         //Create the DocumentBuilderFactory
         DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
         //Create the DocumentBuilder
         DocumentBuilder docbuilder = dbfactory.newDocumentBuilder();
         //Parse the file to create the Document
         mapDoc = docbuilder.parse("mapping.xml" );
         //Instantiate a new Document object
         dataDoc = docbuilder.newDocument();
         //Instantiate the new Document
         newDoc = docbuilder.newDocument();
 
      } catch (Exception e) {
         System.out.println("Problem creating document: "+e.getMessage());
      }
      Element mapRoot = mapDoc.getDocumentElement();
      //Retrieve the (only) data element and cast it to Element
      Node dataNode = mapRoot.getElementsByTagName("data" ).item(0);
      Element dataElement = (Element)dataNode;
      System.out.println(Element.TEXT_NODE);
      //Retrieve the sql statement
      String sql = dataElement.getAttribute("sql" );
      //Output the SQL statement
      System.out.println(sql);
      ResultSetMetaData resultmetadata = null;
      //Create a new element called "data"
      Element dataRoot = dataDoc.createElement("data" );
      try{
      connexion maConnexion = new connexion();
      Class.forName("org.hsqldb.jdbcDriver" );
      String url = maConnexion.getConnexion();
      String user = maConnexion.getUser();
      String pass = maConnexion.getPass();
      bdc=DriverManager.getConnection(url,user,pass);
      Statement monState = bdc.createStatement();
      ResultSet mesRes = monState.executeQuery("select * from client" );
      resultmetadata = mesRes.getMetaData();
      int numCols = resultmetadata.getColumnCount();    
      while (mesRes.next()) {
        //For each row of data
        //Create a new element called "row"
        Element rowEl = dataDoc.createElement("row" );
        for (int i=1; i <= numCols; i++) {
          //For each column, retrieve the name and data
          String colName = resultmetadata.getColumnName(i);
          System.out.println(colName);
          String colVal = mesRes.getString(i);
          System.out.println(colVal);
          //If there was no data, add "and up"
          if (mesRes.wasNull()) {
            colVal = "and up";
          }
          //Create a new element with the same name as the column
          Element dataEl = dataDoc.createElement(colName);
          //Add the data to the new element
          dataEl.appendChild(dataDoc.createTextNode(colVal));
          //Add the new element to the row
          rowEl.appendChild(dataEl);
        }
        //Add the row to the root element
        dataRoot.appendChild(rowEl);
      }
      }
      catch (Exception e) {
         System.out.println("SQL Error: "+e.getMessage());
      }  finally {
         System.out.println("Closing connections..." );
         try {
            bdc.close();
         } catch (SQLException e) {
            System.out.println("Can't close connection." );
         }
      }
      //Add the root element to the document
      dataDoc.appendChild(dataRoot);
      System.out.println("Test1" );
      //Retrieve the root element (also called "root" )
      Element newRootInfo = (Element)mapRoot.getElementsByTagName("root" ).item(0);
      System.out.println("Test2" );
      //Retrieve the root and row information
      String newRootName = newRootInfo.getAttribute("name" );
      System.out.println("Test3" );
      String newRowName = newRootInfo.getAttribute("rowName" );
      System.out.println("Test4" );
      //Retrieve information on elements to be built in the new document
      NodeList newNodesMap = mapRoot.getElementsByTagName("element" );
      System.out.println("Test5" );
      //Create the final root element with the name from the mapping file
      Element newRootElement = newDoc.createElement(newRootName);
      System.out.println("Test6" );
 
      //Retrieve all rows in the old document
      NodeList oldRows = dataRoot.getElementsByTagName("row" );
      System.out.println("Test7" );
      for (int i=0; i < oldRows.getLength(); i++){
 
         //Retrieve each row in turn
         Element thisRow = (Element)oldRows.item(i);
         //Create the new row
         Element newRow = newDoc.createElement(newRowName);
 
         for (int j=0; j < newNodesMap.getLength(); j++) {
 
            //For each node in the new mapping, retrieve the information
            //First the new information...
            Element thisElement = (Element)newNodesMap.item(j);
            System.out.println("Test6" );
            String newElementName = thisElement.getAttribute("name" );
 
            //Then the old information
            Element oldElement = (Element)thisElement.getElementsByTagName("content" ).item(0);
            System.out.println("Test7" );
            String oldField = oldElement.getFirstChild().getNodeValue();
            //Get the original values based on the mapping information
            Element oldValueElement = (Element)thisRow.getElementsByTagName(oldField).item(0);
            System.out.println("Test8" );
            String oldValue = oldValueElement.getFirstChild().getNodeValue();
            System.out.println("Test9" );
            //Create the new element
            Element newElement = newDoc.createElement(newElementName);
            newElement.appendChild(newDoc.createTextNode(oldValue));
            //Retrieve list of new elements
            NodeList newAttributes = thisElement.getElementsByTagName("attribute" );
            System.out.println("Test10" );
            for (int k=0; k < newAttributes.getLength(); k++) {
               //For each new attribute
               //Get the mapping information
               Element thisAttribute = (Element)newAttributes.item(k);
               String oldAttributeField = thisAttribute.getFirstChild().getNodeValue();
               String newAttributeName = thisAttribute.getAttribute("name" );
               //Get the original value
               oldValueElement = (Element)thisRow.getElementsByTagName(oldAttributeField).item(0);
               String oldAttributeValue = oldValueElement.getFirstChild().getNodeValue();
               //Create the new attribute
               newElement.setAttribute(newAttributeName, oldAttributeValue);
            }
            System.out.println("Test11" );
            //Add the new element to the new row
            newRow.appendChild(newElement);
         }
         //Add the new row to the root
         newRootElement.appendChild(newRow);
      }
      //Add the new root to the document
      newDoc.appendChild(newRootElement);
   }
}

n°278852
Profil sup​primé
Posté le 03-01-2003 à 17:00:45  answer
 

:pt1cable:

n°278854
Shogun2002
Posté le 03-01-2003 à 17:04:33  profilanswer
 

voici mon fichier mapping.xml ...
 
Désolé pour ceux qui aiment pas lire ...
 
<?xml version="1.0"?>
<mapping>
   <data sql="select * from client" />
   <root name="lesClients" rowName="client">
      <element name="description">
          <attribute name="id">id</attribute>
          <content>nom</content>
      </element>
      <element name="prenom">
          <content>prenom</content>
      </element>
      <element name="adresse">
          <content>adresse</content>
      </element>
      <element name="cp">
          <content>cp</content>
      </element>
      <element name="ville">
          <content>ville</content>
      </element>
      <element name="pays">
          <content>pays</content>
      </element>
      <element name="tel">
          <content>tel</content>
      </element>
      <element name="email">
          <content>email</content>
      </element>
   </root>
</mapping>

n°278865
darklord
You're welcome
Posté le 03-01-2003 à 17:44:11  profilanswer
 

:pt1cable:


---------------
Just because you feel good does not make you right
n°278884
Shogun2002
Posté le 03-01-2003 à 18:54:41  profilanswer
 

Bon a part ça, y a personne qui connait une methode pour extraire des données d'une BD vers un fichier XML et vice-versa.
 
En utilisant Sax ? non ?

n°278885
Shogun2002
Posté le 03-01-2003 à 18:55:33  profilanswer
 

La méthode la plus simple !
 
Merci

n°279152
_guigui_
Posté le 04-01-2003 à 13:40:09  profilanswer
 

castor XML
il te fait le mapping objet relationnel je crois et te sors les fichiers XML correspondants

n°279195
chichos
Posté le 04-01-2003 à 16:04:39  profilanswer
 

Castor JDO : base de données vers Objet Java (ou l'inverse)
Castor XML : Objet Java vers XML (ou l'inverse)
 
donc ,en combinant les deux, tu peux (assez facilement d'ailleurs) arriver à faire ce que tu veux...
 
@+


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  xml, dom, java

 

Sujets relatifs
comment ajouter une ligne en fin de fichier avec java ??recuperer la resolution d'un ecran en java !! HELP PLEASE
Comment faire pour activé le java ?[Java] Détecter la présence du JRE de Sun
cacher ou iconifier une fentre dos par laquelle j'execute un prog javaFTP et Java
[ java ] CHanger à chaud de JTree c possible ???java trouve pas abs max etc dans java.lang.Math .
Migration de Java 0.1 pré alpha en Java 1.4.1[Java] Prob de connection a une base Oracle [Resolu]
Plus de sujets relatifs à : xml, dom, java


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR