If any one is getting problem while reading xml file in java, following example might help you a lot.
Reading XML file in JAVA using DOM and SAX parser with examples.
For all the java beginners, I am writing a example to read xml file in java using DOM parser and SAX parser.
Here is the xml file.
Reading above xml file in java using DOM parser with example.
OutPut:
Print Root Element :company
+++++++++++++++++++
First Name : Tom
Last Name : Cruse
Nick Name : TC
First Name : Jenny
Last Name : Frank
Nick Name : Jen
Reading above xml java file in java using SAX parser with example.
Start Element :company
Start Element :staff
Start Element :firstname
First Name : Tom
End Element :firstname
Start Element :lastname
Last Name : Cruse
End Element :lastname
Start Element :nickname
Nick Name : TC
End Element :nickname
End Element :staff
Start Element :staff
Start Element :firstname
First Name : Jenny
End Element :firstname
Start Element :lastname
Last Name : Frank
End Element :lastname
Start Element :nickname
Nick Name : Jen
End Element :nickname
End Element :staff
End Element :company
Reading XML file in JAVA using DOM and SAX parser with examples.
For all the java beginners, I am writing a example to read xml file in java using DOM parser and SAX parser.
Here is the xml file.
<company> <staff> <firstname>Tom</firstname> <lastname>Cruse</lastname> <nickname>TC</nickname> </staff> <staff> <firstname>Jenny</firstname> <lastname>Frank</lastname> <nickname>Jen</nickname> </staff> </company>
Reading above xml file in java using DOM parser with example.
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class ReadXMLFileUsingDOMParser { public static void main(String argv[]) { try { File fXmlFileName = new File("c:\\xmlFileName.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFileName); doc.getDocumentElement().normalize(); System.out.println("Print Root Element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("staff"); System.out.println("+++++++++++++++++++"); for (int a = 0; a < nList.getLength(); a++) { Node nNode = nList.item(a); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("First Name : " + getTagValue("firstname",eElement)); System.out.println("Last Name : " + getTagValue("lastname",eElement)); System.out.println("Nick Name : " + getTagValue("nickname",eElement)); } } } catch (Exception e) { e.printStackTrace(); } } private static String getTagValue(String sTag, Element eElement) { NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = (Node) nlList.item(0); return nValue.getNodeValue(); } }
OutPut:
Print Root Element :company
+++++++++++++++++++
First Name : Tom
Last Name : Cruse
Nick Name : TC
First Name : Jenny
Last Name : Frank
Nick Name : Jen
Reading above xml java file in java using SAX parser with example.
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ReadXMLFileUsingSAXParser { public static void main(String args[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean bfname = false; boolean blname = false; boolean bnname = false; boolean bsalary = false; public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException { System.out.println("Start Element :" + qName); if (qName.equalsIgnoreCase("FIRSTNAME")) { bfname = true; } if (qName.equalsIgnoreCase("LASTNAME")) { blname = true; } if (qName.equalsIgnoreCase("NICKNAME")) { bnname = true; } } public void endElement(String uri, String localName,String qName)throws SAXException { System.out.println("End Element :" + qName); } public void characters(char ch[], int start, int length) throws SAXException { if (bfname) { System.out.println("First Name : "+ new String(ch, start, length)); bfname = false; } if (blname) { System.out.println("Last Name : "+ new String(ch, start, length)); blname = false; } if (bnname) { System.out.println("Nick Name : "+ new String(ch, start, length)); bnname = false; } } }; saxParser.parse("c:\\xmlFileName.xml", handler); } catch (Exception e) { e.printStackTrace(); } } }OutPut:
Start Element :company
Start Element :staff
Start Element :firstname
First Name : Tom
End Element :firstname
Start Element :lastname
Last Name : Cruse
End Element :lastname
Start Element :nickname
Nick Name : TC
End Element :nickname
End Element :staff
Start Element :staff
Start Element :firstname
First Name : Jenny
End Element :firstname
Start Element :lastname
Last Name : Frank
End Element :lastname
Start Element :nickname
Nick Name : Jen
End Element :nickname
End Element :staff
End Element :company
nice one, it helps a lot.
ReplyDelete