XML

Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 1
Reputation: thandarucsy is an unknown quantity at this point 
Solved Threads: 0
thandarucsy thandarucsy is offline Offline
Newbie Poster

XML

 
0
  #1
Oct 24th, 2009
Hello, I want to know how to write java code to retrieve content and position of an xml element using sax parser.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 46
Reputation: xml_looser is an unknown quantity at this point 
Solved Threads: 2
xml_looser xml_looser is offline Offline
Light Poster
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #3
Nov 5th, 2009
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.

  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>

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: servicecycle09 is an unknown quantity at this point 
Solved Threads: 1
servicecycle09's Avatar
servicecycle09 servicecycle09 is offline Offline
Newbie Poster
 
0
  #4
7 Days Ago
Thanks a lot well done explanation..
http://www.servicecycle.com - Open source content management, open source outsourcing, tutorials, examples, choosing the correct open source tool, how to outsource software development
Reply With Quote Quick reply to this message  
Reply

Tags
java

Message:



Similar Threads
Other Threads in the XML, XSLT and XPATH Forum


Views: 1016 | Replies: 3
Thread Tools Search this Thread



Tag cloud for java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC