Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses

715 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