
Hi
I have xml data in XMLStreamreader object how to retrive tha data and write into a file using java
Your help will be much appreciated
Thanks

Hi Friend,
Try the following code:
import java.io.*;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class XMLData {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("C:/employee.xml")));
int eventTypeID = reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "Employee");
eventTypeID = reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "Emp_Id");
String id=reader.getElementText();
eventTypeID = reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "Emp_Name");
String name=reader.getElementText();
eventTypeID = reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "Emp_E-mail");
String email=reader.getElementText();
BufferedWriter bw=new BufferedWriter(new FileWriter(new File("C:/emp.txt")));
bw.write(id+" "+name+" "+email);
bw.newLine();
bw.close();
}
}
Hope that it will be helpful for you.
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.