Monday, February 22, 2016

Java Example For Generating Xml Through Xpath


Generating Xml  Through Xpath

PROGRAM

import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.util.Iterator;
import java.util.Vector;
import javax.naming.directory.Attribute;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Example {
   public static void main(String[] args) throws Exception {
      try {
    DocumentBuilderFactory docFactory = null;
 
  Document doc = null;
  String xmlForDislay = null;
  Element rootElement = null;
    docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
rootElement = doc.createElement("student");
  System.out.println("rootElement"+rootElement );
  doc.appendChild(rootElement);
Element student = doc.createElement("studentlist");
rootElement.appendChild(student);
Element rollno = doc.createElement("roollno");
Element name= doc.createElement("name");
Element fname = doc.createElement("fname");
Element lname = doc.createElement("lname");

   rollno.appendChild(doc.createTextNode(""));
name.appendChild(doc.createTextNode(""));
fname.appendChild(doc.createTextNode(""));
lname.appendChild(doc.createTextNode("No Name"));

student.appendChild(rollno);
student.appendChild(name);
student.appendChild(fname);
student.appendChild(lname);

xmlForDislay = getDatafromDocument(doc);
   } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
   } catch (IOException e) {
   e.printStackTrace();
   }
   }

public static String getDatafromDocument(Document doc)throws Exception {
String MED =": getDatafromDocument()";
System.out.println(" called :");
String output =  null;
TransformerFactory tf = null;
Transformer transformer = null;
StringWriter writer = null;
try {
tf = TransformerFactory.newInstance();
transformer = tf.newTransformer();
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
output = writer.getBuffer().toString().replaceAll("\n|\r", "");
System.out.println("output"+output);
}catch(Exception e ){
e.printStackTrace();
System.out.println("message"+e.getMessage());
}finally {
tf = null;
writer = null;
transformer = null;
}
return output;
}
}

Output:

rootElement [student: null]
 called :
output<?xml version="1.0" encoding="UTF-8"?><student><studentlist><roollno></roollno><name></name><fname></fname><lname>No Name</lname></studentlist></student>

Easy Way to Handle Android Notifications

Android Notifications Android Toast class provides a handy way to show users alerts but problem is that these alerts are not persist...