Solved

How to read data from a  xml file and store it in a variable in java?

Posted on 2004-10-07
4
183 Views
Last Modified: 2010-03-31
Hello,

How do I  read data from a read.xml file and store it in a String variable sGroup in a CrewingGroup.java file .

I need to find - 'ost' from - <connection-name>ost</connection-name> in read.xml and
Store it in a variable sGroup in CrewingGroup.java  and use sGroup whereever I have 'ost' hard-coded in the java file.

Example:   'gr-ost' should be 'gr- +  sGroup + "'" // something like that ??

Please provide suggestions on how to do this ..?

Eagerly awaiting suggestions ....
   
File - read.xml
<?xml version="1.0" encoding="UTF-8"?>
<server-list>

        <connection-info>
            <connection-name>ost</connection-name>
            <server-name>ax01</server-name>
            <project-name>ost_test</project-name>
            <project-type>visdev</project-type>
                   <grouping-basename>gr-ost-browseit</grouping-basename>
      </connection-info>
     
</server-list>


File: CrewingGroup.java
import java.sql.*;
import java.lang.*;
import java.io.*;

public class CrewingGroup {

      public static void main (String[] args) throws SQLException, ClassNotFoundException {
             String s_name = "TEST";
            Class.forName ( "oracle.jdbc.driver.OracleDriver" );
            Connection conn = DriverManager.getConnection("jdbc:oracle:oci:@devspi", "scott", "tigre");
            
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery ("select e.str_email1, ge.str_comment from  contact.groups g WHERE  ge.pk_entity = e.pk_entity AND g.str_name = 'gr-ost' ");
            try {
                  FileOutputStream f = null;
                  PrintStream pf = null;
                  f = new FileOutputStream("output.txt");
                  pf = new PrintStream(f);
                  pf.println ("<OrganiseUsers> <sync />");
                  while (rs.next()) {
                        String name = rs.getString(1);
                        s_name = name;
                        String comment = rs.getString(2);
                        System.out.println ("Name : " + name);
                        System.out.println ("Comment : " + comment );
      pf.println ("<move source=\"Users\\LDAP Users\\" + s_name + "\" target=\"Users\\gr-ost\" />");
                  } // end while
                  pf.println ("</OrganiseUsers>");
                  rs.close();
                  conn.close();
                  pf.close(); // close and free up system resource.
                  f.close(); // close in reverse order;FileOutputStream last
            //}
      } catch (Throwable t) {}
           
    } // end main()

}
0
Comment
Question by:sdesar
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Naeemg
ID: 12252949
Dear,
you need to parse xml document using SAX or DOM apis.
if you have Jdk 1.3 or higher, then there will be a package of org.
import that package in ur project.
By using DOM u can easily navigate to each node of xml, and get ur desired data.

Naeem Shehzad Ghuman
0
 

Author Comment

by:sdesar
ID: 12253058
Hello,
I am using J2sdk 1.4.2

Would you please provide the exact code an how to do it?

Thanks

Eagerly awaiting a response.
0
 
LVL 6

Accepted Solution

by:
expertmb earned 50 total points
ID: 12255781
0
 

Author Comment

by:sdesar
ID: 12263901
Thanks expertmb -  Good example - i will check it out.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now