XML and java

Ads
 

XML and java

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

View Answers

January 5, 2011 at 3:39 PM

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

Ads









Related Tutorials/Questions & Answers:
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
Advertisements
java with xml
java with xml  hi i have a problem. // this sample code is reading xml file in java DefaultHandler handler = new DefaultHandler... xml file change in future then my java code will also change. but is there any
XML and java
XML and java  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.
JAVA - XML
JAVA  hi.. i want to talk to any SWT expert in JAVA... how can i do it?   Hi friend, For read more information,Examples and Tutorials on SWT visit to : http://www.roseindia.net/tutorials/swt/ Thanks
xml to html via java
xml to html via java  how to read xml into html using java code
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml...;Please visit the following links: http://www.roseindia.net/tutorial/java/xml
XML in JAVA - Java Beginners
XML in JAVA  hi.. i have an XML file .. and i have parsed... in another file to get all the elements in the documents.. but my java SDK(i.e Eclipse) gives Java Null Pointer exception... can anyone tell me what could be wrong
XMl and Java web services
XMl and Java web services  I want to do a project on java web services and XML. I wanted to ask, how the data entered by user through the HTML file can be converted to an XML file and sent to the server? At the server site, how
Question on java & XML
Question on java & XML  How do we convert the names of files and subfolders into an xml file... Suggestions needed...what are classes we need to use
Java-XML Xerces/Jaxp
Java-XML Xerces/Jaxp  Im new to Java DOM development. I have doubt or I am bit unclear about fact of JAXP and Xerces. My question is why developer use xerces when jaxp does the job
Java-XML Xerces/Jaxp
Java-XML Xerces/Jaxp  Im new to Java DOM development. I have doubt or I am bit unclear about fact of JAXP and Xerces. My question is why developer use xerces when jaxp does the job
how to get this xml syntx in java??????
how to get this xml syntx in java??????  how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
how to get this xml syntx in java??????
how to get this xml syntx in java??????   how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know...://www.roseindia.net/xml/sax/EmployeeDetails.shtml http://www.roseindia.net/java/example/java/xmlParsing/java-xml-parsing-using-sax.shtml http://www.roseindia.net/xml/sax
Java and converting a PDF to XML
Java and converting a PDF to XML  I reviewed your example - https://www.roseindia.net/tutorial/java/xml/pdftoXML.html - and on many users group, it receives high praise. I understand it uses iText but I am having a problem
Java and converting a PDF to XML
Java and converting a PDF to XML  I reviewed your example - https://www.roseindia.net/tutorial/java/xml/pdftoXML.html - and on many users group, it receives high praise. I understand it uses iText but I am having a problem
Version of com.nfbsoftware>java-xml dependency
List of Version of com.nfbsoftware>java-xml dependency
Java Xml Data Store
Java Xml Data Store  I have to do this project and i'm finding it so hard its way over my head if any one has any ideas or help. What would the best... or XML file. As a good Java programmer you will use correct Object-Oriented
java tree expressions - XML
java tree expressions  hi all, i have a problem in tree expressions... to remove that sign. when i click on + button every time it genrates a XML... code to solve the problem and visit to : http://www.roseindia.net/java
XML and swings - Java Beginners
XML and swings  I have an xml file where all the contents that should be present in the tabbed pane are there. i would like to know how to parse the xml such that, whenever it reads the component type as label, it should create
Java XML parser
Java XML parser  Hi friends, i am new to java XML parsing. i need to parse the following xml response which i got from a url hit: <response> <status>SUCCESS</status> <app-config> <CHECK_LOW_SCORE>
How to write to xml file in Java?
How to write to xml file in Java?  Hi Friends, Can anyone help me how to write in xml file in Java programming. Please fill free to give example or reference website for getting example.   Hi, To write in xml file
were to start with xml, XSD and java
where to start with this, since I'm a java developer but never working with xml...were to start with xml, XSD and java  where to start with XML, XSD and java ? my task is to develop an application that read from tcp/ip
Processing XML with Java
Processing XML using Java In this section we will see the API's available in Java for processing the XML. XML file is used on the internet to share... for processing the XML file. The Java XML Processing API provides
how to create xml schema from xml doc using java
how to create xml schema from xml doc using java  i want to create xml schema from xml document programatically using java... am using Netbeans IDE 7.0 i hav created the xml document for a table of data from Database... now i
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
How to pretty print XML from Java?
How to pretty print XML from Java?  How to pretty print XML from Java
XML clonning with java and passing arguments
XML clonning with java and passing arguments  Hi, I have a XML file as follows. My requirements are as below. 1. Clone the whole xml file. 2... if 2 then with arguments1 and then arguments2. <?xml version=ΓΆβ?¬Β?1.0??>
read xml using java
read xml using java  <p>to read multiple attributes and elements from xml in an order.... ex :component name="csl"\layerinterfacefile="poo.c... element to b printed... here is the xml code......................</p> <

Ads