Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
array11 challenge 16 51
create a gui in perl 3 45
HSSFWorkbook cannot be resolved error 10 16
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 …
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

22 Experts available now in Live!

Get 1:1 Help Now