i++){ &
nbsp; Nodenode=
studentList.item(i); &
nbsp; if(node.getNodeType()
==Node.ELEMENT_NODE){ &
nbsp;
System.out
&
nbsp;
.println
("====================="); &
nbsp;
Elemente=(Element)node;
&
nbsp;
NodeListnodeList=e.getElementsByTagName("name");
&
nbsp;
System.out.println("Name:
"
&
nbsp;
+nodeList.item(0).getChildNodes
().item(0)
&
nbsp;
; .getNodeValue()); &
nbsp;
nodeList=e.getElementsByTagName("grade");
&
nbsp;
System.out.println("Grade:
"
&
nbsp;
+nodeList.item(0).getChildNodes
().item(0)
&
nbsp;
; .getNodeValue()); &
nbsp;
nodeList=e.getElementsByTagName("age");
&
nbsp;
System.out.println("Age:
"
&
nbsp;
+nodeList.item(0).getChildNodes
().item(0)
&
nbsp;
; .getNodeValue());
&
nbsp; }
&
nbsp; }
&
nbsp; }else{
&
nbsp; System.exit
(1);
&
nbsp; }
}
}catch(Exceptione)
{
System.out.println(e); } } public
staticvoidmain(String[]args){ XMLParserparser=newXMLParser
(); parser.getAllUserNames("c:
\\test.xml"); }
}
packagenet.viralpatel.java.xmlparser;
importjava.io.File;importjavax.xml.parsers.DocumentBuilder;import
javax.xml.parsers.DocumentBuilderFactory;
importorg.w3c.dom.Document;importorg.w3c.dom.Element;import
org.w3c.dom.Node;importorg.w3c.dom.NodeList;
publicclassXMLParser{
publicvoidgetAllUserNames(StringfileName){try
{DocumentBuilderFactorydbf=
DocumentBuilderFactory.newInstance();DocumentBuilderdb=
dbf.newDocumentBuilder();Filefile=newFile
(fileName);if(file.exists())
{Documentdoc=db.parse
(file);ElementdocEle=doc.getDocumentElement();
//Printrootelementofthe
documentSystem.out.println("Rootelementofthe
document:
"+docEle.getNodeName());
NodeListstudentList=docEle.getElementsByTagName
("student");
//Printtotalstudentelementsin
documentSystem.out&nb
sp;.println("Totalstudents:
"+
studentList.getLength());
if(studentList!
=null&&
studentList.getLength()
>0){for(inti=0;i<
studentList.getLength
();i++){
Nodenode=studentList.item(i);
if(node.getNodeType()==
Node.ELEMENT_NODE){
System.out .println
("=====================");
Elemente=(Element)
node;NodeListnodeList=
e.getElementsByTagName
("name");System.out.println("Name:
"+
nodeList.item(0).getChildNodes().item(0)
.getNodeValue());
nodeList=
e.getElementsByTagName
("grade");System.out.println("Grade:
"+
nodeList.item(0).getChildNodes().item(0)
.getNodeValue());
nodeList=
e.getElementsByTagName
("age");System.out.println("Age:
"+
nodeList.item(0).getChildNodes().item(0)
.getNodeValue());}
}}else
{System.exit
(1);}
}}catch(Exceptione)
{System.out.println(e);}}
publicstaticvoidmain(String[]args){
XMLParserparser=newXMLParser
();parser.getAllUserNames("c:
\\test.xml");}}17.
ConvertArraytoMapinJava
importjava.util.Map; import
mons.lang.ArrayUtils; publicclassMain
{ publicstaticvoidmain(String[]args)
{ String[][]countries={{"UnitedStates",
"NewYork"},{"UnitedKingdom",
"London"}, {
"Netherland","Amsterdam"},{"Japan","Tokyo"},{"France","Paris"}
}; MapcountryCapitals=
ArrayUtils.toMap(countries); System.out.println("CapitalofJapanis"+countryCapitals.get
("Japan")); System.out.println("Capitalof
Franceis"+countryCapitals.get("France")); }
}
importjava.util.Map;importmons.lang.ArrayUtils;
publicclassMain{
publicstaticvoidmain(String[]args){String[][]
countries={{"UnitedStates","NewYork"},{"UnitedKingdom",
"London"},{"Netherland",
"Amsterdam"},{"Japan","Tokyo"},{"France","Paris"}
};
MapcountryCapitals=ArrayUtils.toMap(countries);
System.out.println("CapitalofJapanis"+
countryCapitals.get("Japan"));System.out.println("Capitalof
Franceis"+countryCapitals.get("France"));}}18.SendEmail
usingJava
importjavax.mail.*; import
javax.mail.internet.*; importjava.util.*; publicvoidpostMail(Stringrecipients[],Stringsubject,Stringmessage,
String
from)throwsMessagingException {
booleandebug=false; //Setthehostsmtpaddress
Propertiesprops=newProperties();
props.put("mail.smtp.host",
""); //createsome
propertiesandgetthedefaultSession Session
session=Session.getDefaultInstance(props,null);
session.setDebug(debug); //createamessage Messagemsg=newMimeMessage(session); //setthefromandtoaddress
InternetAddressaddressFrom=newInternetAddress
(from); msg.setFrom(addressFrom);
InternetAddress[]addressTo=new
InternetAddress[recipients.length]; for(inti=
0;iaddressTo[i]=newInternetAddress
(recipients[i]); }
msg.setRecipients(Message.RecipientType.TO,
addressTo); //Optional:
Youcan
alsosetyourcustomheadersintheEmailifyouWant
msg.addHeader("MyHeaderName","myHeaderValue");
//SettingtheSubjectandContent
Type msg.setSubject(s