How can i read XML string in a loop???

i changed xml file reader to xml string reader by
looking around this and other forums.
here is the code.
public class XMLTester2 {
    public static void main(String[] args) {
        String s = "<firsttag><secondtag>123</secondtag></firsttag>";
        java.io.Reader reader = new java.io.StringReader(s);
        org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader);
        org.w3c.dom.Document doc = null;
        javax.xml.parsers.DocumentBuilderFactory fact = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        try {
            javax.xml.parsers.DocumentBuilder builder = fact.newDocumentBuilder();
            doc = builder.parse(source);
        } catch (javax.xml.parsers.ParserConfigurationException pce) {
        } catch (org.xml.sax.SAXException se) {
        } catch (java.io.IOException ioe) {
        } finally {
            try {
                reader.close();
            } catch (java.io.IOException ignored) {
}i put this stuff into a loop, but i always ends up
having new in the loop.
            String inputLine = "Start";
            // define what's needed for XML messages
            Reader reader = new StringReader(inputLine);
            InputSource source = new InputSource(reader);
            Document doc = null;
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = null;
            Node xmlNode = null;
            try {
                builder = fact.newDocumentBuilder();
            } catch (ParserConfigurationException pce) {
            // -->
            while ( (inputLine = in.readLine()) != null) {
                System.out.println("readLine() : {" + inputLine + "}");
                reader = new StringReader(inputLine);
                source.setCharacterStream(reader);
                try {
                    doc = builder.parse(source);
                } catch (SAXException se) {
                    System.out.println("SAXException : " + se);
                    System.out.println("SAXException source : " + source.toString() );
                    se.printStackTrace();
                } catch (IOException ioe) {
                    System.out.println("IOException : " + ioe);
                } finally {
                    try {
                        reader.close();
                    } catch (IOException ignored) {
                reader = null;i know putting new in the loop is VERY bad.
i gotto get ride of this new from the loop.
can anyone help me?

i know putting new in the loop is VERY bad.
i gotto get ride of this new from the loop.Don't design your application based on one-liners you heard somewhere. Do some timings to see if it matters. My guess would be that parsing an XML file would take a fair bit longer than creating a StringReader object. And you do need a new StringReader each time, they aren't reusable, so the alternatives are not obvious. And don't accept my guess about the timings, check it for yourself.

Similar Messages

  • How can i read a string with nextToken() of StreamTokenizer

    I need for my class paper to read a string from a file and i used the StreamTokenizer's method nextToken but i can not read a string with it. my code is:
    StreamTokenizer st = new StreamTokenizer(the code that gets the input from the file)
    String line;
    while ((line !=br.readLine()) != null) {
    String surname = (st.nextToken()).trim();
    but it gets me some error of:
    int can not be dereferenced
    what should I do to get the string i need?

    Look at the API for java.io.StreamTokenizer. In particular, look at the return type for nextToken().
    Good luck.

  • How can i read XML from local drive using Javascript into Live Cycle

    Hello,
    I am creating PDF Form using Acrobat LiveCycle.
    I want to write java script for load data into relative field and data present into xml file which is located at local drive
    I have write all method for parsing xml which is required ............... but i am stuck into how to read/open/load local drive xml using javascript?

    sandyrock:
    You'd better post this message to Adobe LiveCycle forum: http://www.adobeforums.com/webx/.3bbeda8d/
    You can use postMessage method in LiveCycle JavaScript to communicate with host application,the host application can be a web brower like IE.
    In IE you can use IE javascript to access local XML file.

  • How can I read the printer snapshot file printers.xml

    How can I read the printer snapshot file printers.xml

    XML files are typically viewed using a web browser.
    Open the XML file and the default program should open it automatically.  If you are prompted to select a program, use the option that lets you choose a program and brows through the directories for your browser (for example, use Internet Explorer, Firefox, etc.).
    If need be, you should be able to right-click the file and select open with to choose the program to open it with.
    There are other XML viewers out ther, but web browsers are typically the default as XML is a web language.
    ↙-----------How do I give Kudos?| How do I mark a post as Solved? ----------------↓

  • How can i generate xml like this?

    Hi all,
    How can i generate xml like this & i need to send it to via HTTP :
    <mms>
                 <subject>message subject</subject>
                 <url_image>http://image_url</url_image>
                 <url_sound>http://sound_url</url_sound>
                 <url_video>http://video_url</url_video>
                 <text>message text</text>
                 <msisdn_sender>6281XYYYYYY</msisdn_sender>
                 <msisdn_receipient>6281XYYYYYY</msisdn_receipient>
                 <sid>to be define later</sid>
                 <trx_id>Unique number</trx_id>
                 <trx_date>yyyyMMddHHmmss</trx_date>
                 <contentid>see note</contentid>
    </mms>& how can i get the value of the sid (for example)?
    I hav tried to generate that xml by using StringBuffer & append, but it's not what i mean...
    Anyone can help me?

    Ok...i got it. But i still hav some problems.
    This is the sample code that i used :
    public class XMLCreator {
         //No generics
         List myData;
         Document dom;
            Element rootEle, mmsEle, mmsE;
            StringWriter stringOut;
            mms mms;
         public XMLCreator(String subject, String image, String sound,
                    String video, String text, String sender, String recipient,
                    int id, String date, String contentid) {
              mms = new mms(subject, image, sound, video, text, sender,
                            recipient, id, contentid, date);
                    createDocument();
         public void run(){
              createDOMTree();
              print();
         private void createDocument() {
              //get an instance of factory
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              try {
              //get an instance of builder
              DocumentBuilder db = dbf.newDocumentBuilder();
              //create an instance of DOM
              dom = db.newDocument();
              }catch(ParserConfigurationException pce) {
                   //dump it
                   System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
         private void createDOMTree(){
              //create the root element <Mms>
              rootEle = dom.createElement("mms");
              dom.appendChild(rootEle);
              createMmsElement(mms);
         private Element createMmsElement(mms b){
              Element subjectEle = dom.createElement("subject");
              Text subjectText = dom.createTextNode(b.getSubject());
              subjectEle.appendChild(subjectText);
              rootEle.appendChild(subjectEle);
              //create url_image element and author text node and attach it to mmsElement
              Element imageEle = dom.createElement("url_image");
              Text imageText = dom.createTextNode(b.getUrl_image());
              imageEle.appendChild(imageText);
              rootEle.appendChild(imageEle);
              // & etc....
              return rootEle;
          * This method uses Xerces specific classes
          * prints the XML document to file.
         private void print(){
              try
                   //print
                   OutputFormat format = new OutputFormat(dom);
                   format.setIndenting(true);
                            stringOut = new StringWriter();
                   //to generate output to console use this serializer
                   XMLSerializer serializer = new XMLSerializer(stringOut, format);
                   //to generate a file output use fileoutputstream instead of system.out
                   //XMLSerializer serializer = new XMLSerializer(
                   //new FileOutputStream(new File("mms.xml")), format);
                   serializer.serialize(dom);
              } catch(IOException ie) {
                  ie.printStackTrace();
            public String getStringOut() {
                return stringOut.toString();
    }when i tried to show the stringOut.toString() in my jsp, it's only showed string like this :
    The Lords Of The Ring http://localhost:8084/movie/lotr.3gp 6281321488448 6281321488448 123 0 20070220114851 LOTR.
    1. Why this is happen?i want to generate xml which its format is like above.
    2. How can i send this xml (put in msg parameter) using jsp (via web) without creating the mms.xml?
    3. if i want to set the msg parameter equal to mms.xml - means that msg = mms.xml, what is the data type for msg? is it an object or anything else?
    Thx b4 in advance...

  • How can I send multiple string commands into a VISA write?

    Hi Fellow LabVIEW users
    I am very new to LabVIEW (2.5 months) so please forgive me if my lingo is not up to par.
    How can I send multiple string commands to a VISA write. For example each string command looks like this
    1) 3A00 0000 0000 FFFF FFFF FFFF 0000 0000 FF00 0000 0000 0000 0000 0033 (Scenario 1)
    2) 3A01 0000 0000 FFFF FFFF FFFF 0000 0000 FF00 0000 0000 0000 0000 0034 (Scenario 2)
    3) 3A01 0000 0000 33FF FFFF FFFF 0000 0000 FF00 0000 0000 0000 0000 0067 (Scenario 3).
    and so on and so forth. And there are a number of scenarios.
    Each String scenario will give a different string output which will be interpreted differently in the front panel.
    Right now I have to manually change the string commands on the front panel to give me the desired output. How can I do this without manually changing the commands i.e. hard coding each scenario into the block diagram?
    Thanks, any feedback will help.
    mhaque

    Please stick to your original post.

  • How can I access xml document from javascript whithin a JSP page

    how can I access xml document from javascript whithin a JSP page?
    I have a JSP that receives an XML document from a JavaBean, so I can access it within the entire JSP, but I need to access it from the javascript inside the JSP... and I have no idea how i can do this.
    Thanks in advance!

    The solution would only work on MS IE browsers, as other browsers do not support an XML DOM.
    It can be done, but you would be stuck with using the Microsoft broswer. If that is acceptable, I have some example code, and a book recommendation.

  • How can i read a stored picture in oracle Long Raw datatype? blob or clob?

    How can i read a stored picture in oracle Long Raw datatype? Like a blob or clob?....i am using jdk 1.3
    This is because...i tried to read it like a blob but i obtain a exception...about Type of column no valid......but the column exist....and it contains the long raw datatype of the pictures.....this is my code:
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import oracle.jdbc.driver.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.InputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.DriverManager;
    import oracle.sql.BLOB;
    import oracle.sql.BLOB.*;
    import oracle.jdbc.driver.*;
    import java.sql.*;
    class rec_ima1
    public static void main(String h[])
    Connection con = null;
    Blob bl;
    final ImageIcon image1;
    JPanel photo;
    try
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    con= DriverManager.getConnection("jdbc:oracle:thin:@123.3.12.213:1521:db_name","user","password");
    String query = "Select * from pictures where ID = '18840'";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery( query );
    if (!rs.next())
    System.out.println("Empty Result Set");
    bl = rs.getBlob(5);
    if (bl == null) {
    System.out.println("Null Blob");
    return;
    InputStream is = bl.getBinaryStream();
    int imageLength = (int) bl.length();
    System.out.println(imageLength);
    System.out.println(bl.length());
    byte[] imageData = new byte [imageLength];
    is.read(imageData, 0, imageLength);
    image1 = new ImageIcon(imageData);
    photo = new JPanel() {
    public void paint(Graphics g){
    g.setColor(Color.lightGray);
    g.drawImage(image1.getImage(), 0, 0, this);
    } catch (Exception e) {
    e.printStackTrace();
    Now i tried using clob:
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import oracle.jdbc.driver.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.InputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.DriverManager;
    import oracle.sql.CLOB;
    import oracle.sql.CLOB.*;
    import oracle.jdbc.driver.*;
    import java.sql.CallableStatement;
    class rec_ima4
    public static void main(String h[])
    Connection con = null;
    Clob cl;
    JPanel photo;
    try
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    con= DriverManager.getConnection("jdbc:oracle:thin:@123.3.12.213:1521:db_name","user","password");
    con.setAutoCommit (false);
    String query = "Select * from pictures where ID = '18840'";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery( query );
    while (rs.next()) {
    oracle.sql.CLOB clob = (CLOB) rs.getObject(5); //line 47
    } catch (Exception e) {
    e.printStackTrace();
    This is the runtime exception:
    java.lang.ClassCastException: [B
    at rec_ima4.main(rec_ima4.java:47)

    Thanks by answering to me......
    Well....i did that....but what is ImageIO?....
    I declared a ImageIcon imageIO, but this give me the following:
    rec_ima3.java:49: cannot resolve symbol
    symbol : class BufferedImage
    location: class rec_ima3
    BufferedImage bi = ImageIO.read(bInput);
    ^
    rec_ima3.java:49: cannot resolve symbol
    symbol : variable ImageIO
    location: class rec_ima3
    BufferedImage bi = ImageIO.read(bInput);
    ^
    What classes i have to import?.....what is ImageIO?
    Thanks

  • How can I read a file with ASCII Special Character into a SQL table using SSIS 2008?

    I've tried everything to read this file and am getting no where.   Help how can I read this file and load a SQL table?
    RS - AscII - 30  (Record Separator)
    GS - AscII - 29 (Group Separator)
    Thank you for your assistance - Covi
    Mark Covian

    We can use script component as source/transformation to read the text file and assign the contains to a string. Split the string by chr(30)  i.e RS and finally stored into an array or write to the output buffer of the script component.
    For example on how to use script component refer this link
    http://social.technet.microsoft.com/Forums/en-US/6ff2007d-d246-4107-b77c-624781baab38/how-to-use-substring-in-derived-column-in-ssis?forum=sqlintegrationservices
    Regards, RSingh

  • Using Lab view ver 6,How can I read a cell of excel file right after I write to it

    How can I read a specif cell of an Excel file using Labview VI.

    Hi,
    Attached is a LV6.1 VI which will read a cell.
    It will be looking for a sub VI found in the example C:\Program Files\National Instruments\LabVIEW\examples\comm\ExcelExamples.ll​b.
    The returned value is a string value but there is no reason why it couldn't be a number. Just connect a numeric to the type connector of the Variant to Data function.
    Hope this helps.
    Regards
    Ray Farmer
    Regards
    Ray Farmer
    Attachments:
    Get_Cell_Value.vi ‏41 KB
    Write_Table_To_XL.vi ‏101 KB

  • How can I seperate one string into multiple strings using PowerShell?

    New to PowerShell...how can I read/separate parts of a file name? For instance if I have a file name like "part1_part2_part3_part4.xls", how to separate this file name to four different parts? So I basically want to go from that file name,
    to four different strings like part1, part2, part3, and part4...as four separate strings. How can I get this done in PowerShell?
    Thanks

    You can try something like this
    PS> (Get-ChildItem part1_part2_part3_part4.xls).BaseName.Split('_')

  • How can I read and write text in rings that are inside an array?

    Hello All!!!
    How can I read and write text in rings that are inside an array?
    Regards and thanks in advance.

    Use a Property Node linked to the Ring inside the array.
    Of course, all elements in the array will have the same text values.
    B-)
    Message Edited by LabViewGuruWannabe on 12-13-2007 09:47 AM
    Message Edited by LabViewGuruWannabe on 12-13-2007 09:48 AM
    Message Edited by LabViewGuruWannabe on 12-13-2007 09:49 AM
    Attachments:
    Strings-BD.PNG ‏17 KB
    Strings-FP1.PNG ‏23 KB

  • How to get an XML string store in CLOB or LONG column ?

    How to get an XML string store in CLOB or LONG column ?
    We use XSU with the following command
    String str = qry.getXMLString();
    but all the "<" are replace by "&lt;"
    It's impossible to parse the result for XSLT transformation
    Thank's for your help
    Denis Calvayrac
    Example :
    in the column "TT_NAME"
    "<name><firstname>aaa</<firstname><lastname>bbb</lastname></name>
    I want this result
    <TT_NAME>
    <name>
    <firstname>aaa</firstname>
    <lastname>bbb</lastname>
    </name>
    </TT_NAME>
    but, I have this result
    <TT_NAME>
    &lt;name&gt;
    &lt;firstname&gt;aaa&lt;/firstname&gt;
    &lt;lastname&gt;bbb&lt;/lastname&gt;
    &lt;/name&gt;
    </TT_NAME>

    Can you post some of your code, so I can take a look ?
    Thanks

  • How can i read Object serialized in delphi ?

    anyone has got any ideas how can i read serialized object created by delphi and was serialized in a file

    zeda wrote:
    Peter__Lawrey wrote:
    To my knowledge you need a Delphi program to read it.so there is no 3rd party plugin or so?Not that I know of. You could learn more about how Delphi serializes objects, and work back from there. But you're probably much better off taking a hint from Peter and serializing to XML in the first place. This sort of problem is the exact kind of thing XML is good at solving

  • How can I transform XML-DB to Relational DB?

    How can I transform XML-DB to Relational DB?
    I want to transform Oracle 9i XML-DB to Oracle 8i R-db
    please tell me how to do this .
    thank you

    hi
    good
    its idoc_xml or idoc_xml_transform
    go through the below link hope it ll help you to solve your problem
    Check with below link :
    Re: IDOCS_OUTPUT_IN_XML_FORMAT -- IDOCS_OUTPUT_TO_FILE
    Re: any function module to write-xml schema of a idoctype to an internal table
    /people/michal.krawczyk2/blog/2005/11/13/xi-how-to-check-your-idocs-on-a-web-page-from-sapgui -> this will help you
    thanks
    mrutyun^

Maybe you are looking for

  • How can I change the font in the page menu across the top?

    I would like to change the font and its color in the page menu across the top of the website I'm building. The inspector doesn't give me the option nor does the font menu.

  • Flattening a pdf-file

    In the last version of acrobat reader for the ipad, I can "flatten" a pdf-file before emailing. (only then!) It seems to me that the "flattening' is not really "flattening" the pdf-file ! When I open this pdf-file on my computer with acrobat reader,

  • Using response.sendDirect

    i need to redirect to the next page without user clikcin anythin and at e same time pass in 2 parameter to the next page, i used response.sendRedirect("processEventRegCheque.jsp?teventid=eventid+tpay=pay"); it doesnt work cos it concat eventidtpay=pa

  • Trying to upload photo to Facebook cover.  Message says I need premier.  I have Premier Elements 13.

    Trying to upload photo to Facebook cover.  Gets message saying I need Premier Elements.  I HAVE Adobe Photoshop Premier Elements 13.  What is the problem?

  • Retrieving applications after a system restore.

    Today, my little brother was playing around and accidentally restored my iPod Touch. So, I retrieved all of my songs, music videos, and shows. But WHERE are my applications? ._. I'd really rather not spend hours upon hours attempting to remember ever