In this tutorial we are going to see how to parse xml and json files. first we will just parse a xml file and later we will parse xml elements to objects
1) for java we are using DOM parser
xml file to be parsed
<?xml version="1.0"?>
<company>
<employee id="1001">
<firstname>yong</firstname>
<lastname>mna</lastname>
<nickname>mrreferenc</nickname>
<salary>100000</salary>
</employee>
<employee id="2001">
<firstname>dev</firstname>
<lastname>journal</lastname>
<nickname>general</nickname>
<salary>200000</salary>
</employee>
<employee id="2002">
<firstname>darth</firstname>
<lastname>vader</lastname>
<nickname>anakan</nickname>
<salary>2000</salary>
</employee>
<employee id="2003">
<firstname>obi</firstname>
<lastname>van kanobi</lastname>
<nickname>ben kanobi</nickname>
<salary>1500</salary>
</employee>
<employee id="2009">
<firstname>hello</firstname>
<lastname>world</lastname>
<nickname>wasteland</nickname>
<salary>20</salary>
</employee>
</company>
and the coe to parse the nodes and attributes were
package xmlparsing;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Domparsing {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
File file= new File("/home/solomon/solo.xml");
System.out.println(file.exists());
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
NodeList companylist= doc.getElementsByTagName("employee");
for(int i=0;i<companylist.getLength();i++){
Node companynoes=companylist.item(i);
System.out.println(companynoes.getTextContent());
if(companynoes.hasAttributes()){
NamedNodeMap attributes = companynoes.getAttributes();
for(int j=0;j<attributes.getLength();j++){
System.out.println("attribut values"+attributes.getNamedItem("id").getNodeValue());
}
}
}
}
}
and the output is
true
yong
mna
mrreferenc
100000
attribut values1001
dev
journal
general
200000
attribut values2001
darth
vader
anakan
2000
attribut values2002
obi
van kanobi
ben kanobi
1500
attribut values2003
hello
world
wasteland
20
attribut values2009
XPATH;
now we use get the firstname of all employees whose salary is greater than 1500
1)create an xpath object
XPath xpath= XPathFactory.newInstance().newXPath();
private static void getdata(Document doc, XPath path){
List<String>salarybyname= new ArrayList<>();
try {
XPathExpression expression = path.compile("/company/employee[salary>"+1500+"]/firstname/text()");
NodeList nodelist=(NodeList)expression.evaluate(doc, XPathConstants.NODESET);
System.out.println(nodelist.getLength());
for(int i=0;i<nodelist.getLength();i++){
System.out.println(nodelist.item(i).getTextContent());
}
System.out.println("Number of items added to the list"+salarybyname.size());
for(String employee:salarybyname){
System.out.println(employee);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
e.printStackTrace();
}
}
and the o/p is
yong
dev
darth
1) for java we are using DOM parser
xml file to be parsed
<?xml version="1.0"?>
<company>
<employee id="1001">
<firstname>yong</firstname>
<lastname>mna</lastname>
<nickname>mrreferenc</nickname>
<salary>100000</salary>
</employee>
<employee id="2001">
<firstname>dev</firstname>
<lastname>journal</lastname>
<nickname>general</nickname>
<salary>200000</salary>
</employee>
<employee id="2002">
<firstname>darth</firstname>
<lastname>vader</lastname>
<nickname>anakan</nickname>
<salary>2000</salary>
</employee>
<employee id="2003">
<firstname>obi</firstname>
<lastname>van kanobi</lastname>
<nickname>ben kanobi</nickname>
<salary>1500</salary>
</employee>
<employee id="2009">
<firstname>hello</firstname>
<lastname>world</lastname>
<nickname>wasteland</nickname>
<salary>20</salary>
</employee>
</company>
and the coe to parse the nodes and attributes were
package xmlparsing;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Domparsing {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
File file= new File("/home/solomon/solo.xml");
System.out.println(file.exists());
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
NodeList companylist= doc.getElementsByTagName("employee");
for(int i=0;i<companylist.getLength();i++){
Node companynoes=companylist.item(i);
System.out.println(companynoes.getTextContent());
if(companynoes.hasAttributes()){
NamedNodeMap attributes = companynoes.getAttributes();
for(int j=0;j<attributes.getLength();j++){
System.out.println("attribut values"+attributes.getNamedItem("id").getNodeValue());
}
}
}
}
}
and the output is
true
yong
mna
mrreferenc
100000
attribut values1001
dev
journal
general
200000
attribut values2001
darth
vader
anakan
2000
attribut values2002
obi
van kanobi
ben kanobi
1500
attribut values2003
hello
world
wasteland
20
attribut values2009
now we use get the firstname of all employees whose salary is greater than 1500
1)create an xpath object
XPath xpath= XPathFactory.newInstance().newXPath();
private static void getdata(Document doc, XPath path){
List<String>salarybyname= new ArrayList<>();
try {
XPathExpression expression = path.compile("/company/employee[salary>"+1500+"]/firstname/text()");
NodeList nodelist=(NodeList)expression.evaluate(doc, XPathConstants.NODESET);
System.out.println(nodelist.getLength());
for(int i=0;i<nodelist.getLength();i++){
System.out.println(nodelist.item(i).getTextContent());
}
System.out.println("Number of items added to the list"+salarybyname.size());
for(String employee:salarybyname){
System.out.println(employee);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
e.printStackTrace();
}
}
and the o/p is
yong
dev
darth