Results 1 to 5 of 5
  1. #1
    karthik84 is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default How to parse an xml file using java

    Hello frnds,
    I am new to java and xml programming.
    Now i have to write a java code for searching the xml document with a name and replace that tag value with a new one
    how can we do that?
    my java program is :

    import java.io.FileOutputStream;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JButton;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import java.util.Properties;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;

    private void btn_clickmeActionPerformed(java.awt.event.ActionEv ent evt) {
    String a = txt_query.getText();
    //System.out.println(a);

    try
    {
    OutputStream fout= new FileOutputStream("C:/student.xml");
    OutputStream bout= new BufferedOutputStream(fout);
    OutputStreamWriter out = new OutputStreamWriter(bout, "8859_1");
    out.write("<?xml version=\"1.0\" ");
    out.write("encoding=\"ISO-8859-1\"?>\r\n");
    out.write("<student>\r\n");
    for (int j = 0; j <= 2; j++) {
    out.write("<stud_name>" +b[j]);
    out.write("</stud_name>\r\n");
    out.write("<stud_age>"+n[j]);
    out.write("</stud_age>\n");
    }
    out.write("</student>\r\n");
    out.flush(); // Don't forget to flush!
    out.close();
    }
    catch (UnsupportedEncodingException e) {
    System.out.println(
    "This VM does not support the Latin-1 character set."
    );
    }
    catch (IOException e) {
    System.out.println(e.getMessage());
    }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    //String b[]=;//declaration
    //int n
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new student().setVisible(true);
    }
    });

    }

    // Variables declaration - do not modify
    private javax.swing.JButton btn_clickme;
    private javax.swing.JTextField replace;
    private javax.swing.JTextField txt_query;
    // End of variables declaration

    private String b[]={"Suresh","karthik","asldkj"};

    private int n[]= {25,26,27};

    }
    and my xml file is:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <student>
    <stud_name>Suresh</stud_name>
    <stud_age>25</stud_age>
    <stud_name>karthik</stud_name>
    <stud_age>26</stud_age>
    <stud_name>asldkj</stud_name>
    <stud_age>27</stud_age>
    </student>


    Reply soon...........
    Thank u all,
    Chanti.
    Last edited by karthik84; 08-18-2008 at 12:41 PM. Reason: Help Urgent

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    It's better if you can specifically explain your question here. Because most of the members easy to give a hint depend on that.

  3. #3
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    use the dom parser to parse the xml file.. it is easy one...

    use the api's of dom parser..

    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

  4. #4
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    there are other parsers as well like xalan

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    There are lots of parser available for xml passing in Java. Thing here is the question is not specifically explain. If the thread starter not response here for a long, no point keep open this thread.

Similar Threads

  1. Parse EDI File Using java
    By vaskarbasak in forum Advanced Java
    Replies: 6
    Last Post: 09-24-2008, 02:38 PM
  2. Replies: 7
    Last Post: 05-23-2008, 03:46 AM
  3. SAX Parse
    By bluefloyd8 in forum New To Java
    Replies: 1
    Last Post: 01-25-2008, 03:57 PM
  4. how to parse an xml file
    By oregon in forum XML
    Replies: 3
    Last Post: 08-01-2007, 04:53 PM
  5. How to parse in html
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •