944,072 Members | Top Members by Rank

Ad:
Oct 24th, 2009
0

XML

Expand Post »
Hello, I want to know how to write java code to retrieve content and position of an xml element using sax parser.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thandarucsy is offline Offline
1 posts
since Oct 2009
Reputation Points: 16
Solved Threads: 21
Junior Poster
xml_looser is offline Offline
178 posts
since Apr 2009
Nov 5th, 2009
0
Re: XML
To use SAX, you must implement the interface org.xml.sax.ContentHAndler. Typically you accomplish this by exending org.xml.sax.DefaultHAndler class because it provides default implementations for all the callbacks in the SAX handler classes. Then you override the methods of the interface and specify which action will be taken as the various parts of the XML document are encountered.

xml Syntax (Toggle Plain Text)
  1. <?xml version="1.0" standalone="yes"?>
  2. <person>
  3. <name>Rajesh</name>
  4. <phone type="home">1229999</phone>
  5. <email>rajesh@yahoo.co.in</email>
  6. </person>

java Syntax (Toggle Plain Text)
  1. import java.io.IOException;
  2.  
  3. import javax.xml.parsers.SAXParser;
  4. import javax.xml.parsers.SAXParserFactory;
  5.  
  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.ContentHandler;
  8. import org.xml.sax.ErrorHandler;
  9. import org.xml.sax.InputSource;
  10. import org.xml.sax.Locator;
  11. import org.xml.sax.SAXException;
  12. import org.xml.sax.SAXParseException;
  13. import org.xml.sax.XMLReader;
  14.  
  15. public class SAXDump {
  16. static public void main(String[] arg) {
  17. String filename = null;
  18.  
  19. if (arg.length == 1) {
  20. filename = arg[0];
  21. } else {
  22. usage();
  23. }
  24.  
  25. // Create a new factory that will create the parser.
  26. SAXParserFactory spf = SAXParserFactory.newInstance();
  27.  
  28. // Create the XMLReader to be used to parse the document.
  29. XMLReader reader = null;
  30. try {
  31. SAXParser parser = spf.newSAXParser();
  32. reader = parser.getXMLReader();
  33. } catch (Exception e) {
  34. System.err.println(e);
  35. System.exit(1);
  36. }
  37.  
  38. // Specify the error handler and the content handler.
  39. reader.setErrorHandler(new MyErrorHandler());
  40. reader.setContentHandler(new MyContentHandler());
  41. // Use the XMLReader to parse the entire file.
  42. try {
  43. InputSource is = new InputSource(filename);
  44. reader.parse(is);
  45. } catch (SAXException e) {
  46. System.exit(1);
  47. } catch (IOException e) {
  48. System.err.println(e);
  49. System.exit(1);
  50. }
  51. }
  52.  
  53. private static void usage() {
  54. System.err.println("Usage: SAXDump <filename>");
  55. System.exit(1);
  56. }
  57. }
  58.  
  59. class MyContentHandler implements ContentHandler {
  60. private Locator locator;
  61.  
  62. /**
  63.   * The name and of the SAX document and the current location within the
  64.   * document.
  65.   */
  66. public void setDocumentLocator(Locator locator) {
  67. this.locator = locator;
  68. System.out.println("-" + locator.getLineNumber() + "---Document ID: "
  69. + locator.getSystemId());
  70. }
  71.  
  72. /** The parsing of a document has started.. */
  73.  
  74. public void startDocument() {
  75. System.out.println("-" + locator.getLineNumber()
  76. + "---Document parse started");
  77. }
  78.  
  79. /** The parsing of a document has completed.. */
  80. public void endDocument() {
  81. System.out.println("-" + locator.getLineNumber()
  82. + "---Document parse ended");
  83. }
  84.  
  85. /** The start of a namespace scope */
  86. public void startPrefixMapping(String prefix, String uri) {
  87. System.out.println("-" + locator.getLineNumber()
  88. + "---Namespace scope begins");
  89. System.out.println(" " + prefix + "=\"" + uri + "\"");
  90. }
  91.  
  92. /** The end of a namespace scope */
  93. public void endPrefixMapping(String prefix) {
  94. System.out.println("-" + locator.getLineNumber()
  95. + "---Namespace scope ends");
  96. System.out.println(" " + prefix);
  97. }
  98.  
  99. /** The opening tag of an element. */
  100. public void startElement(String namespaceURI, String localName,
  101. String qName, Attributes atts) {
  102. System.out.println("-" + locator.getLineNumber()
  103. + "---Opening tag of an element");
  104. System.out.println(" Namespace: " + namespaceURI);
  105. System.out.println(" Local name: " + localName);
  106. System.out.println(" Qualified name: " + qName);
  107. for (int i = 0; i < atts.getLength(); i++) {
  108. System.out.println(" Attribute: " + atts.getQName(i) + "=\""
  109. + atts.getValue(i) + "\"");
  110. }
  111. }
  112.  
  113. /** The closing tag of an element. */
  114. public void endElement(String namespaceURI, String localName, String qName) {
  115. System.out.println("-" + locator.getLineNumber()
  116. + "---Closing tag of an element");
  117. System.out.println(" Namespace: " + namespaceURI);
  118. System.out.println(" Local name: " + localName);
  119. System.out.println(" Qualified name: " + qName);
  120. }
  121.  
  122. /** Character data. */
  123. public void characters(char[] ch, int start, int length) {
  124. System.out.println("-" + locator.getLineNumber() + "---Character data");
  125. showCharacters(ch, start, length);
  126. }
  127.  
  128. /** Ignorable whitespace character data. */
  129. public void ignorableWhitespace(char[] ch, int start, int length) {
  130. System.out.println("-" + locator.getLineNumber() + "---Whitespace");
  131. showCharacters(ch, start, length);
  132. }
  133.  
  134. /** Processing Instruction */
  135. public void processingInstruction(String target, String data) {
  136. System.out.println("-" + locator.getLineNumber()
  137. + "---Processing Instruction");
  138. System.out.println(" Target: " + target);
  139. System.out.println(" Data: " + data);
  140. }
  141.  
  142. /** A skipped entity. */
  143. public void skippedEntity(String name) {
  144. System.out.println("-" + locator.getLineNumber() + "---Skipped Entity");
  145. System.out.println(" Name: " + name);
  146. }
  147.  
  148. /**
  149.   * Internal method to format arrays of characters so the special whitespace
  150.   * characters will show.
  151.   */
  152. public void showCharacters(char[] ch, int start, int length) {
  153. System.out.print(" \"");
  154. for (int i = start; i < start + length; i++)
  155. switch (ch[i]) {
  156. case '\n':
  157. System.out.print("\\n");
  158. break;
  159. case '\r':
  160. System.out.print("\\r");
  161. break;
  162. case '\t':
  163. System.out.print("\\t");
  164. break;
  165. default:
  166. System.out.print(ch[i]);
  167. break;
  168. }
  169. System.out.println("\"");
  170. }
  171. }
  172.  
  173. class MyErrorHandler implements ErrorHandler {
  174. public void warning(SAXParseException e) throws SAXException {
  175. show("Warning", e);
  176. throw (e);
  177. }
  178.  
  179. public void error(SAXParseException e) throws SAXException {
  180. show("Error", e);
  181. throw (e);
  182. }
  183.  
  184. public void fatalError(SAXParseException e) throws SAXException {
  185. show("Fatal Error", e);
  186. throw (e);
  187. }
  188.  
  189. private void show(String type, SAXParseException e) {
  190. System.out.println(type + ": " + e.getMessage());
  191. System.out.println("Line " + e.getLineNumber() + " Column "
  192. + e.getColumnNumber());
  193. System.out.println("System ID: " + e.getSystemId());
  194. }
  195. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Dec 14th, 2009
0
Re: XML
Thanks a lot well done explanation..
Reputation Points: 10
Solved Threads: 2
Newbie Poster
servicecycle09 is offline Offline
19 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: Help with xsl:collection
Next Thread in XML, XSLT and XPATH Forum Timeline: XML to Linq returning Length? - Newbie!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC