how to store XML Document in MS SQL using JAVA

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nata

    how to store XML Document in MS SQL using JAVA

    hi all
    i am trying to store the XML Document object into MS SQL2005
    i want to store this as whole not different tables;
    plz help me regarding this i am trying form last two weeks i asked many
    people but no use.
    below is my program In that i want to store Document document object
    into database which as var image/blob/clob plz help me




    import javax.xml.parse rs.DocumentBuil der;
    import javax.xml.parse rs.DocumentBuil derFactory;
    import javax.xml.parse rs.FactoryConfi gurationError;
    import javax.xml.parse rs.ParserConfig urationExceptio n;
    import org.w3c.dom.Doc ument;
    import org.w3c.dom.DOM Exception;
    import org.w3c.dom.Ele ment;
    import java.util.*;
    import java.io.*;
    import java.sql.*;
    import java.io.File;
    import java.io.FileNot FoundException;
    import java.io.FileRea der;
    import java.lang.Chara cter;


    /*class input implements Serializable
    {
    byte[] toByteArray()
    {
    ByteArrayOutput Stream ob=new ByteArrayOutput Stream(100);
    try
    {
    ObjectOutputStr eam oob=new ObjectOutputStr eam(ob);
    oob.writeObject (this);
    }catch(Exceptio n e)
    {System.out.pri ntln("HeartBeat to Byte Conversion Error");}

    if(len<0)
    {
    byte b[];
    b=ob.toByteArra y();
    len=b.length;
    return b;
    }
    else return ob.toByteArray( );
    }
    }*/
    class TestXml implements Serializable
    {
    public static String getFileData(byt e [] data) {
    System.out.prin tln("\nIN GET FILE DATA");
    String tmpString=null;
    for (int i = 0; i < data.length; i++)
    {
    char c = Character.toCha rs(data[i])[0];
    tmpString += c;
    System.out.prin t(c);
    }
    return tmpString;
    }
    public static void main(String[] args) throws SQLException,IO Exception

    {
    // input in=new input();
    org.w3c.dom.Doc ument document;
    Document doc;
    DocumentBuilder Factory factory =
    DocumentBuilder Factory.newInst ance();
    try {
    DocumentBuilder builder = factory.newDocu mentBuilder();
    document = builder.newDocu ment(); // Create from whole cloth
    doc = builder.newDocu ment();
    Element root =
    (Element) document.create Element("rootEl ement");
    document.append Child(root);
    Element Child1 =
    (Element) document.create Element("Child1 ");
    Element Child2 =
    (Element) document.create Element("Child2 ");
    root.appendChil d( document.create TextNode("Some" ) );
    root.appendChil d( document.create TextNode(" ") );
    root.appendChil d( document.create TextNode("text" ) );

    Child1.appendCh ild( document.create TextNode("Some" ) );
    Child1.appendCh ild( document.create TextNode(" ") );
    Child1.appendCh ild( document.create TextNode("text" ) );
    Child2.appendCh ild( document.create TextNode("Some" ) );
    Child2.appendCh ild( document.create TextNode(" ") );
    Child2.appendCh ild( document.create TextNode("text" ) );
    root.appendChil d(Child1);
    root.appendChil d(Child2);
    // System.out.prin tln(JDBCUtil.se rialize(documen t));
    Foo f=new Foo();
    ConnectionObjec t con=new ConnectionObjec t();
    Connection co=con.GetConne ction();
    Statement st = co.createStatem ent();
    st.executeUpdat e("Insert into XML1
    values('"+docum ent.getDocument Element()+"');" );
    String str="Nataraju";
    System.out.prin tln(str.getByte s());
    Statement st1 = co.createStatem ent();
    ResultSet rs=st1.executeQ uery("select * from XML1;");
    Element root1;
    while(rs.next() )
    {
    // BufferedReader br=new BufferedReader( rs.getAsciiStre am(1));
    /* byte b1[]=(rs.getAsciiSt ream(1)).getByt es();
    ByteArrayInputS tream b=new ByteArrayInputS tream(b1);
    int c;
    while((c=b.read ())!=-1)
    {
    System.out.prin t((char)c);
    b.reset();
    }*/
    byte [] data = rs.getBytes(1);
    String Str=getFileData (data);
    System.out.prin tln(Str);

    }

    co.close();

    }
    catch (ParserConfigur ationException pce) {
    // Parser with specified options can't be built
    pce.printStackT race();

    }
    }
    }

Working...