How to search information from XML document using SAX parser

How to search information from XML document using SAX parser

XML (eXtensible Markup Language) is platform independent file having hierarchical structure of elements to hold meaningful data. Parser is used to read, search, and edit information from XML document as XML document will do anything on its own.Simple API for XML (SAX) is one of the parser. It is sequential event driven, fast, memory intensive, read only API available to deal with XML. Follow these steps:

1. Creating XML document
 <?xml version="1.0"?>
<addressbook>
 <record>
  <name>C.Chandran</name>
  <city>Koimattur</city>
 </record>
 <record>
  <name>Prathmesh</name>
  <city>Ichalkaranji</city>
 </record>
</addressbook> 


2. Java Program to search information
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import java.util.*;
import java.io.*;

public class searchxml extends DefaultHandler
{
 static Scanner sc=new Scanner(new InputStreamReader(System.in));
 int flag=0;
 String temp;
 static String categary;
 static String skey;
 public void startElement(String uri, String local, String row, Attributes attrs) throws SAXException
 {
 }
 public void characters(char []ch, int start, int len) throws SAXException
 {
  temp=new String(ch,start,len);
 }
 public void endElement(String uri, String local, String row) throws SAXException
 {
  if(row.equals(categary) && temp.equals(skey))
  {
   System.out.println("Record Found.");
   flag=1;
  }
 }
 public void endDocument() throws SAXException
 {
  if(flag==0)
  {
   System.out.println("\nRecord NOT Found.");
  }
 }
 public static void main(String argd[])
 {
  try
  {
   searchxml sx=new searchxml(); 
   System.out.print("\n\nEnter Categary : ");
   categary=sc.next();
   System.out.print("\nEnter Search Key : ");
   skey=sc.next(); 
   SAXParserFactory factory = SAXParserFactory.newInstance(); 
   SAXParser saxParser = factory.newSAXParser();
   saxParser.parse("addressbook.xml",sx);
   
  }catch(Exception e)
  {
   System.out.print(e); 
  }
 }
}



3. Output
Enter Category: name
Enter Search Key: Prathmesh
Record Found
How to send email from localhost XAMPP server's gmail account to email address using PHPMailer

How to send email from localhost XAMPP server's gmail account to email address using PHPMailer

Trying to send email from localhost XAMPP server using gmail account to specified email address. Follow the following steps.

1. Download PHPmailer and extract it to applications home directory e.g.C:\xampp\htdocs\MyApp.

2. Copy the following program and save it with mail.php
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                      // Mailer uses SMTP to send mail
$mail->Host='smtp.gmail.com';                         // Host  
$mail->SMTPAuth = true;                               // Allowing SMTP authentication
$mail->Username = 'test@gmail.com';                   // SMTP username
$mail->Password = 'Test123!';                         // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption
$mail->Port = 587;                                    // TCP port to connect to
$mail->setFrom('test@gmail.com');                     // From address
$mail->addAddress('test2@gmail.net');                 // Add a recipient
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Mail Subject';
$mail->Body    = 'Body of email';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
This PHP script uses gmail's SMTP host to send mail with SMTP authentication. This SMTP authentication needs your gmail account's username and password. For secure transmission of mail use TLS or SSL. For TLS use 587 as port number and for SSL use 465. Then create message by setting sender's, receiver's email id along with subject and message body.

3. Now set "less secure app" to sender's email account so that account can send email to other email ids. For this first login in gmail account and then search "less secure app" in google. After clicking on less secure app link in search result just on "Allow less secure app" option

4. But if still not able to send mail then download this script and paste in extracted folder of phpmailer for allowing SMTP server to send mail.

Note : For sending e-mail make sure that network is proxy free.