Saturday, 10 January 2015

Parsing an XML file with DOM in Java

Below program describes that, reading the data from network xml resource. And it describe how to get particular  node value and display it in console.

In java we have by default javax.xml library which contains the several class to manage the Document Object Model (xml) data. But it is too lengthy. By using the third party libraries such as JDOM etc we can manage the xml content very easily.


package files;

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXML {

public static void main(String[] args) {

try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse("http://services.explorecalifornia.org/rss/tours.php");
NodeList nodeList = doc.getElementsByTagName("title");

System.out.println("Total elements " +nodeList.getLength()+" are presented");

for (int i = 0; i < nodeList.getLength(); i++) {

Element item = (Element)nodeList.item(i);

System.out.println(item.getFirstChild().getNodeValue());

}
}catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}

Reading a text file from a networked resource

Reading the content from network resource

package files;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class ReadNetworkFile {

public static void main(String[] args) {

try {
URL url = new URL("http://services.explorecalifornia.org/rss/tours.php");

InputStream in = url.openStream();
BufferedInputStream bis = new BufferedInputStream(in);

StringBuilder sb = new StringBuilder();

while (true) {
int val = bis.read(); //reading byte by byte

if (val == -1){ // -1 represents it reached EOF
break;
}else{
sb.append((char)val);
}
}

System.out.println(sb);

} catch (IOException e) {
e.printStackTrace();
}

}

}

Managing files with Apache Commons FileUtils


Managing files with Apache Commons FileUtils, First we need to download commons-io-2.4.jar file from the above site. Add the jar file to eclipse java build path.

I have written the simple file reading/writing  program

package files;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class Main {

public static void main(String[] args) {
try {
File f1 = new File("loremipsum.txt");
File f2 = new File("target.txt");

FileUtils.copyFile(f1, f2);

System.out.println("File copied!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Simple way to read and write files in java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFile {

public static void main(String[] args) {

try {
File f1 = new File("loremipsum.txt");
File f2 = new File("target.txt");

InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);

byte[] buf = new byte[1024];

int len;

while (((len = in.read(buf)) > 0)) {

out.write(buf, 0, len);

}

in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}