how to search and replace a tag value in xml file usng java
Questions about XML that are not covered by the other forums should go here.
-
- Posts: 1
- Joined: Mon Aug 18, 2008 8:48 am
how to search and replace a tag value in xml file usng java
Hello frnds,
I am new to java and xml programming.
i have a problem ie.
i have to write a java program which searches a value for a tag and replace that value with a new value.
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;
/*
* student.java
*
* Created on August 12, 2008, 6:00 PM
*/
/**
*
* @author bharadwaj
*/
public class student extends javax.swing.JFrame {
/** Creates new form student */
public student() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
txt_query = new javax.swing.JTextField();
btn_clickme = new javax.swing.JButton();
replace = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btn_clickme.setText("Search");
btn_clickme.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_clickmeActionPerformed(evt);
}
});
replace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
replaceActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(36, 36, 36)
.add(txt_query, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(16, 16, 16)
.add(replace, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 98, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(111, 111, 111)
.add(btn_clickme)))
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(21, 21, 21)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(txt_query, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(replace, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(23, 23, 23)
.add(btn_clickme)
.addContainerGap(39, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void replaceActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btn_clickmeActionPerformed(java.awt.event.ActionEvent 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.
I am new to java and xml programming.
i have a problem ie.
i have to write a java program which searches a value for a tag and replace that value with a new value.
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;
/*
* student.java
*
* Created on August 12, 2008, 6:00 PM
*/
/**
*
* @author bharadwaj
*/
public class student extends javax.swing.JFrame {
/** Creates new form student */
public student() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
txt_query = new javax.swing.JTextField();
btn_clickme = new javax.swing.JButton();
replace = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btn_clickme.setText("Search");
btn_clickme.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_clickmeActionPerformed(evt);
}
});
replace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
replaceActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(36, 36, 36)
.add(txt_query, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(16, 16, 16)
.add(replace, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 98, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(111, 111, 111)
.add(btn_clickme)))
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(21, 21, 21)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(txt_query, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(replace, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(23, 23, 23)
.add(btn_clickme)
.addContainerGap(39, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void replaceActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btn_clickmeActionPerformed(java.awt.event.ActionEvent 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.

-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Re: how to search and replace a tag value in xml file usng java
Post by sorin_ristache »
Hello,
Sorry, this is not a Java development forum. You should post your question to a Java forum.
Regards,
Sorin
Sorry, this is not a Java development forum. You should post your question to a Java forum.
Regards,
Sorin
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service