Discussions

XML & Web services: XML Parsing from a string

  1. XML Parsing from a string (4 messages)

    Hi,
    i am fetching xml file from the database into a string and try to parse the string,it does not.it gives me an error
    i am uning the DOMParser class and its method parse which can accept either a file or a string but i keep get this error


    C:\Mahesh\BlueEdge>java testxml
    select qq_xml from quote_qt where qq_id = 1 for update
    output length : 35
    output : <quote><name>qt1235</name></quote>
    org.xml.sax.SAXParseException: File " <quote><name>qt1235</name></quote>" not found.
    output1 : <quote><name>qt1235</name></quote>

    does anyone have an idea whats happening

    Thanks,
    mahesh

    Threaded Messages (4)

  2. XML Parsing from a string[ Go to top ]

    Your not fetching a file form the database, you are fetching, what is basically a string. You are then giving this to the sax parser, but this is expecting a file name, not the contents. I'm not sure of the API but it must be straight forward to give it an input stream rather than a file.
  3. XML Parsing from a string[ Go to top ]

    could u put your code here?

    vijay
  4. It would be better if you put your code here.But,firstly you see what input are you giving to SAX parser-is it the name of the file or a String?If you passing String as a input parameter then this may be the cause of exception.Secondly, never forget that the xml declaration:
    <?xml version="1.0" ?>, should be first part of your String.
    Lastly, better convert the String into byte stream and wrap it in InputSource object.
      
  5. Thanks for your attention,i was able to solve the problem,i wrapped the string into an input source and it works,basically the methos parse needs an input source which could be a file or a string,now i am stuck with another problem,when i try retrieving the string back from the parser after it is updated using the method getXMLString it throws me an error saying

    C:\Mahesh\BlueEdge>javac testxml.java
    testxml.java:105: getXMLString() has protected access in org.apache.xerces.framework.XMLParser
                            System.out.println("XML String :" + ps1.getXMLString());
                                                                   ^
    1 error

    please advice,find below the code
    Thanks,
    Mahesh

    import com.ibm.xml.parser.*;
    import org.apache.xerces.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import Beans.DataAccess.*;
    import java.sql.*;
    import java.io.*;

    class testxml {
    public static void main (String args[])
    {
    String xmlfile = "";
    Connection conn;
    java.sql.Clob c1 = null;
    java.sql.Clob c2 = null;
    try{

    String url = "jdbc:oracle:thin:@172.23.100.11:1521:tbew";
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    conn = DriverManager.getConnection(url,"beweb","beweb");
    conn.setAutoCommit(false);

                Statement stmt = conn.createStatement();
    String sql = "select qq_xml from quote_qt where qq_id = 1 for update";
    System.out.println(sql);
    ResultSet rst = stmt.executeQuery(sql);


    while ( rst.next() ) {
    c1 =rst.getClob("qq_xml");
    java.io.InputStream readClobis = c1.getAsciiStream();
    //char[] c = new char[26];
    System.out.println("output length : " + c1.length());
    for (int i=0 ; i < c1.length() ; i++) {
    xmlfile = xmlfile + (char) readClobis.read();
    //System.out.println("output : " + i );
    }
    System.out.println("output : " + xmlfile );
    xmlfile = parseXML(xmlfile);
    System.out.println("output1 : " + xmlfile );
    java.io.OutputStream osss = ((oracle.sql.CLOB) c1).getAsciiOutputStream();
    System.out.println("output1 : " + xmlfile );
    byte[] bss = xmlfile.getBytes("ASCII");
    osss.write(bss);
    osss.flush();
           }

    conn.commit();

    //while (rst1.next()){
    // System.out.println("inside");
    // c2 = rst1.getClob("qq_xml");
    //Reader charstr = c1.getCharacterStream();
    //System.out.println("Clob Data:" + c1.getSubString((long)0,(int)c1.length()));


    }
    catch(SQLException ee){System.out.println(ee);}
    catch(java.lang.ClassNotFoundException ee1){System.out.println(ee1);}
    catch(java.lang.InstantiationException ee2){System.out.println(ee2);}
    catch(java.lang.IllegalAccessException ee3){System.out.println(ee3);}
    catch(java.io.UnsupportedEncodingException ee4){System.out.println(ee4);}
    catch(java.io.IOException ee5){System.out.println(ee5);}



    }

    static String parseXML(String xmldata){

    try{
    DOMParser ps1 = new DOMParser();
    Reader reader = new BufferedReader(new StringReader(xmldata));
    InputSource source = new InputSource(reader);
    ps1.parse(source);
    //ps1.parse("quote.xml");
    Document doc1 = ps1.getDocument();
    Element e1 = doc1.getDocumentElement();
    e1.getTagName();
    System.out.println("doc root:" + e1.getTagName());

    NodeList n1 = e1.getChildNodes();

    for (int i = 0; i < n1.getLength();i++)
    {
    System.out.println(n1.item(i).getNodeName());

    NodeList n2 = n1.item(i).getChildNodes();
    for (int j = 0; j < n2.getLength();j++)
    {
    System.out.println(n2.item(j).getNodeName());
    System.out.println(n2.item(j).getFirstChild().getNodeValue());
    if (n1.item(i).getNodeName().equals("proposal")){
    if (n2.item(j).getNodeName().equals("covletter")){
    System.out.println("Inside");
    n2.item(j).getFirstChild().setNodeValue("no");
    System.out.println(n2.item(j).getFirstChild().getNodeValue());
    }
    }

    }
    }
    System.out.println("XML String :" + ps1.getXMLString());

    }
    catch(SAXException e){ System.out.println(e);}
    catch(Exception ee){ System.out.println(ee);}
    return xmldata;

    }

    }