Java XML Tutorial
Selected Reading
- Java XML - Discussion
- Java XML - Useful Resources
- Java XML - Quick Guide
- Java XML - Questions and Answers
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM4J Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java XPath Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java StAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- JDOM XML Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java SAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM Parser
- Java XML Parsers
- Java XML Overview
- Java XML Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Query XML Document
Java SAX Parser - Query XML Document
Demo Example
Here is the input text file that we need to Query for rollno: 393
<?xml version = "1.0"?> <class> <student rollno = "393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>95</marks> </student> <student rollno = "593"> <firstname>jasvir</firstname> <lastname>singn</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class>
UserHandler.java
package com.tutorialspoint.xml; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; pubpc class UserHandler extends DefaultHandler { boolean bFirstName = false; boolean bLastName = false; boolean bNickName = false; boolean bMarks = false; String rollNo = null; @Override pubpc void startElement( String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("student")) { rollNo = attributes.getValue("rollno"); } if(("393").equals(rollNo) && qName.equalsIgnoreCase("student")) { System.out.println("Start Element :" + qName); } if (qName.equalsIgnoreCase("firstname")) { bFirstName = true; } else if (qName.equalsIgnoreCase("lastname")) { bLastName = true; } else if (qName.equalsIgnoreCase("nickname")) { bNickName = true; } else if (qName.equalsIgnoreCase("marks")) { bMarks = true; } } @Override pubpc void endElement( String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")) { if(("393").equals(rollNo) && qName.equalsIgnoreCase("student")) System.out.println("End Element :" + qName); } } @Override pubpc void characters(char ch[], int start, int length) throws SAXException { if (bFirstName && ("393").equals(rollNo)) { //age element, set Employee age System.out.println("First Name: " + new String(ch, start, length)); bFirstName = false; } else if (bLastName && ("393").equals(rollNo)) { System.out.println("Last Name: " + new String(ch, start, length)); bLastName = false; } else if (bNickName && ("393").equals(rollNo)) { System.out.println("Nick Name: " + new String(ch, start, length)); bNickName = false; } else if (bMarks && ("393").equals(rollNo)) { System.out.println("Marks: " + new String(ch, start, length)); bMarks = false; } } }
SAXQueryDemo.java
package com.tutorialspoint.xml; import java.io.File; 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; pubpc class SAXQueryDemo { pubpc static void main(String[] args) { try { File inputFile = new File("input.txt"); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); UserHandler userhandler = new UserHandler(); saxParser.parse(inputFile, userhandler); } catch (Exception e) { e.printStackTrace(); } } } class UserHandler extends DefaultHandler { boolean bFirstName = false; boolean bLastName = false; boolean bNickName = false; boolean bMarks = false; String rollNo = null; @Override pubpc void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("student")) { rollNo = attributes.getValue("rollno"); } if(("393").equals(rollNo) && qName.equalsIgnoreCase("student")) { System.out.println("Start Element :" + qName); } if (qName.equalsIgnoreCase("firstname")) { bFirstName = true; } else if (qName.equalsIgnoreCase("lastname")) { bLastName = true; } else if (qName.equalsIgnoreCase("nickname")) { bNickName = true; } else if (qName.equalsIgnoreCase("marks")) { bMarks = true; } } @Override pubpc void endElement( String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")) { if(("393").equals(rollNo) && qName.equalsIgnoreCase("student")) System.out.println("End Element :" + qName); } } @Override pubpc void characters( char ch[], int start, int length) throws SAXException { if (bFirstName && ("393").equals(rollNo)) { //age element, set Employee age System.out.println("First Name: " + new String(ch, start, length)); bFirstName = false; } else if (bLastName && ("393").equals(rollNo)) { System.out.println("Last Name: " + new String(ch, start, length)); bLastName = false; } else if (bNickName && ("393").equals(rollNo)) { System.out.println("Nick Name: " + new String(ch, start, length)); bNickName = false; } else if (bMarks && ("393").equals(rollNo)) { System.out.println("Marks: " + new String(ch, start, length)); bMarks = false; } } }
This would produce the following result −
Start Element :student First Name: dinkar Last Name: kad Nick Name: dinkar Marks: 85 End Element :studentAdvertisements