Solved

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

Posted on 2004-10-07
4
184 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
servlet  URL Rewriting 1 36
tomcat startup error 5 63
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

770 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