how to read and write an xml file using java

how to read and write an xml file using java

Hi

Can anyone help me how to read and write an xml file which has CData using java

View Answers

December 15, 2010 at 3:31 PM

Hi Friend,

Try the following code:

1) CreateCDATA.java:

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class CreateCDATA {
    public static void main(String[] args) {
        String xml =
                "<root>" +
                "   <messages>" +
                "       <message></message>" +
                "   </messages>" +
                "</root>";

        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(new StringReader(xml));
            Element root = document.getRootElement();
            Element comments = root.getChild("messages");

            Element comment = comments.getChild("message");
            comment.setContent(new CDATA("Hello World"));
            Writer writer=new FileWriter(new File("new.xml"));
            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            outputter.output(document, writer);
            } 
            catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2)ReadCDATA.java:

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class ReadCDATA {
  public static void main(String[] args) throws Exception {
    File file = new File("new.xml");
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(file);

    NodeList nodes = doc.getElementsByTagName("messages");
    for (int i = 0; i < nodes.getLength(); i++) {
      Element element = (Element) nodes.item(i);
      NodeList title = element.getElementsByTagName("message");
      Element line = (Element) title.item(0);
      System.out.println("Message: " + getCharacterDataFromElement(line));
        }
  }
  public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
      CharacterData cd = (CharacterData) child;
      return cd.getData();
    }
    return "";
  }
}

Thanks


January 11, 2011 at 5:46 PM

how to read a xml file(login file with username and password)verify it and if it is correct pass the control to home page.









Related Tutorials/Questions & Answers:
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
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... = window.adsbygoogle || []).push({}); To write in xml file in java program
Advertisements
To read & write a excel file using the core java
To read & write a excel file using the core java  Hai, I'm new to JavaProgram.But now i need java program to read & write a excel file so, can anyone help me to learn the above mentioned topic(link for the portion
simple code to write an read and write the login detail to a xml file using javascript ( username and password )
simple code to write an read and write the login detail to a xml file using... to write and read the login details (username and password )into a xml file using javascript. (XML database
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access Xml which is present in my d drive plz can u should go through my code n tell me the things where i went wrong java servlet program protected void
how to read this xml file - XML
how to read this xml file  i want to read this xml file using java(using struts2 or using jsp) and i want result as name=admin menu=user... read i have tried lot more , but i am not able to read this xml file
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
How to read value from xml using java?
How to read value from xml using java?  Hi All, I want to read value from following xml using java.. In <Line>,data is in format of key and value pair.. i want to read only values..could u plz help me in this?Thanks
how to read and write xml files in javascript
how to read and write xml files in javascript  hi m very new to javascript and xml,, now i want to read and write xml files in javascript,, please give me a sample code to do
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know that there are many ways to read using different API like SAX, DOM , XOM Jar...Read XML using Java  Hi All, Good Morning, I have been working
write a progam for bubble sort using file read nd write?
write a progam for bubble sort using file read nd write?  hi, please give the code
how to read file using InputStreamReader in java
how to read file using InputStreamReader in java  Hi, I want to learn to use the InputStreamReader class of Java and trying to read a text file with the class. how to read file using InputStreamReader in java? Thanks  
Java file read write operation
Java file read write operation  how to read and write the data from text file.Suppose i have text file with 4 fields name ,roll no ,marks1,marks2...(8000); oos.writeObject("Ankit"); oos.writeObject("Java Developer
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...;   Please visit the following link: Read XML data
How to Split a large XML file using java?
How to Split a large XML file using java?  How can we split a 500MB Xml file?I know how to split xml file after reading the entire document in a file.Here we cannot load the entire file as it is a large file
How to write to file using FileOutputStream
How to write to file using FileOutputStream  Hi friends, Please help me in java program. How to write to file using FileOutputStream..., To write a file using FileOutputStream, we have to use FileOutputStream class
How to write to file using FileWriter
How to write to file using FileWriter  hi, How to write to file...({});   Hi, To writing in a file in Java program we uses a class... be created using the following of its constructor i.e. FileWriter(File file
how to write greater than symbol in a file using java
how to write greater than symbol in a file using java  Hello I want to write < and > symbol in a file but its coming like > and < Please... and write the CData from an xml file but not able to print the value of CData
how to use Excel Templet to write excel file using java.
how to use Excel Templet to write excel file using java.  how to use Excel Templet to write excel file using java
Read and write file
Read and write file  HI, How to read and write file from Java program? Thanks   Hi, See the following tutorials: Java Write To File Read File Thanks
how to use Excel Template to write excel file using java
how to use Excel Template to write excel file using java  How to use Excel template to write data in that file using java
read/write to Windows Registry using Java
read/write to Windows Registry using Java  read/write to Windows Registry using Java
JDOM example in java, How to read a xml file in java.
JDOM example in java, How to read a xml file in java. In this tutorial, we will see how to  read a xml file in java. Code. Student.xml...;File(data);       DOMBuilder builder 
how to write java data - XML
how to write java data  how to write data to xml file  Hi friend, Read for more information, http://www.roseindia.net/xml/dom/ Thanks
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji..._TO_REPLACE_1 raji fail how to write the program
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java... to write in the file. For more details and examples related to How to write in File... some file we require it to be open in append mode by using
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java. Please suggest and give example of this program. thanks. (adsbygoogle = window.adsbygoogle || []).push
How read xml file value in dojox chart - XML
How read xml file value in dojox chart  How to read the xml file value in dojox chart
How to Retrieve Data from the database and write into excel file using Java
How to Retrieve Data from the database and write into excel file using Java  Hi, I am trying to develop an small application where i trying to retrieve Data from the database and store the details in excel file. Please can
how to read file line by line using filereader in java
how to read file line by line using filereader in java  Hi, how to read file line by line using filereader in java? Thanks   Hi, This is example code: package my.code; import java.io.BufferedReader; import
How to Write to file in Java?
How to Write to file in Java?  How to Write to file in Java Program... to write to File in Java Program: (adsbygoogle = window.adsbygoogle... BufferedWriter(fw); br.write("This example demonstrates you how to write in a file
how to write in xml? - XML
how to write in xml?  can anybody give the code how to write in xml file. i wan to write inside the tag, so what i have to do? Thanks...  ...(new InputStreamReader(System.in)); System.out.print("Enter XML file name
How to Read a file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... problem is to find the best way to read the file in Java. I just searched the google... = window.adsbygoogle || []).push({}); So, help me and let's know How to Read a file
can't read my xml file in java
read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read the xml file?thx...can't read my xml file in java  i've a xml file like this : <
can't read my xml file in java
read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read the xml file?thx...can't read my xml file in java  i've a xml file like this : <
can't read my xml file in java
read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read the xml file?thx...can't read my xml file in java  i've a xml file like this : <
can't read my xml file in java
read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read the xml file?thx...can't read my xml file in java  i've a xml file like this : <
can't read my xml file in java
read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read the xml file?thx...can't read my xml file in java  i've a xml file like this : <
can't read my xml file in java
read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read the xml file?thx...can't read my xml file in java  i've a xml file like this : <
How to Read file line by line in Java program
by line in Java. But there are various ways that can help read a larger file... in example ?inputStream.close()?. The output of read file line by line in Java...({}); Example of Read file line by line using BufferReader: package ReadFile
How to write a file in Java?
How to write a file in Java? To write a file in Java use the class FileWriter... and strings. Example of how to write text to a file in java... a text file by the name "out.txt" and will write "Hello Java
How to write file by line in Java
How to write file by line in Java  How to write file by line in Java   Hi, For Write to a file by line using java programming language we...({}); How to use Write To File By Line in Java programs
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java   import org.w3c.dom.*; import org.w3c.dom.Node; import javax.xml.parsers.
How to Write To File BufferedWriter in Java
How to Write To File BufferedWriter in Java  how to write to file bufferedwriter in Java   In Java there are 2 types of class files used... for your references. How to use Bufferedwriter class in Java programs
how to write append file in Java
how to write append file in Java  How to write append file in Java...({}); For More Details visit this link: How to Append file in Java... existing file should be retained when you are trying to write new contents
Java program to read a text file and write to another file
How to Write to a File in Java without overwriting? Java read...Java program to read a text file and write to another file - Creating.... Our requirement is to read a text file and then write the content of the text
How to write into CSV file in Java
How to write into CSV file in Java  How to write into CSV file in Java   Hi, To create Comma separated value(CSV) file is very simple... || []).push({}); How to create File to CSV in Java
How to generate xml file using xpath
How to generate xml file using xpath  Hi, I have a requirement... to make a xml using that,. It would be great is you can help me out with how to make a xml using xpath. Thanks in Advance, SanjayADS_TO_REPLACE_1   Hi
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml

Ads