Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-10-07
4
Medium Priority
?
189 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 200 total points
ID: 12255781
0
 

Author Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

926 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