Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Java and XML

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 28-07-2010
Member
 
Join Date: Apr 2010
Posts: 73
Java and XML
  

After what has been said in the title Java and XML, I am having doubt on the parser SAX (Simple API for XML). I mention this because SAX was the first adopted as an XML parser API for XML in Java. So the main reason of posting here is to gather information about the Java and XML related topics. As can be seen along the post, SAX leaves much to be desired with respect to other XML APIs such as DOM-based. Also please provide me some more information about the same.

Reply With Quote
  #2  
Old 28-07-2010
Member
 
Join Date: Mar 2008
Posts: 192
Re: Java and XML

You should know about the example driver which is important from Java and XML point of view.
Example Driver - It is the class that will process each event to launch the SAX processor. Just inherit the default SAX handler DefaultHandler and overwrite the methods for the desired events. In this case I have overwritten the most common:
  • startDocument: occurs when you start processing the XML document.
  • EndDocument: come at the end of the XML document processing.
  • startElement: start occurs when processing an xml tag. This is where you read the tag attributes.
  • EndElement: This occurs at the end of processing an xml tag.
  • characters: there is to find a text string.
Reply With Quote
  #3  
Old 28-07-2010
Member
 
Join Date: Mar 2008
Posts: 232
Re: Java and XML

Whenever the parser finds a tag will call startElement. Whether the label is. Receive as a parameter the name of the label but not the parent tag. So if we have nested tags with the same name, we must be stored in a persistent object (at least for the processing of the xml document) the route by which we label processing. That is, an object that stores the state in which it is processing the document. This disadvantage is accentuated when reading the labels and values that the characters method is called without specifying the element to which you are accessing. To find out which label is the text we should have passed previously marked by the corresponding startElement. In this case it is essential to the object that marks the state.
Reply With Quote
  #4  
Old 28-07-2010
Member
 
Join Date: Apr 2008
Posts: 193
Re: Java and XML

For this kind of thing say that this API is arcane and outdated. When we see DOM, we will see that you can have random access to any part of the XML document.
Code:
MyDriver.java 

  org.xml.sax.Attributes import; 
  import org.xml.sax.SAXException; 
  org.xml.sax.helpers.DefaultHandler import; 

  / ** 
  * SAX event handler example. 
  * 
  * @ Author Xela 
  * 
  * / 
  DefaultHandler MyDriver public class extends ( 

  @ Override 
  public void startDocument () throws SAXException ( 
  System.out.println ("\ nPrinciple document ..."); 
  ) 

  @ Override 
  public void EndDocument () throws SAXException ( 
  System.out.println ("\ nFin document ..."); 
  ) 

  @ Override 
  public void startElement (String uri, String localName, String name, 
  Attributes attributes) throws SAXException ( 
  System.out.println ("\ nProcesando tag ..."); 
  System.out.println ("\ tNamespace uri:" + uri); 
  System.out.println ("\ tfirst_name:" + localName); 
  System.out.println ("\ tfirst_name with prefix:" + name); 

  / / Loop through the attributes 
  System.out.println ("\ tProcesando" + attributes.getLength () + "attributes ..."); 
  for (int i = 0; i <attributes.getLength (); i + +) ( 
  System.out.println ("\ t \ tfirst_name:" + attributes.getQName (i)); 
  System.out.println ("\ t \ tValor:" + attributes.getValue (i)); 
  ) 

  // Also we can get the attributes by name 
  String value = attributes.getValue ("id"); 
  if (value! = null) ( 
  System.out.println ("\ tid:" + value); 
  ) 

  ) 

  @ Override 
  public void characters (char [] ch, int start, int length) 
  throws SAXException ( 
  System.out.println ("\ nProcesando text within a label ..."); 
  System.out.println ("\ TText:" + String.valueOf (ch, start, length)); 
  ) 

  @ Override 
  public void EndElement (String uri, String localName, String name) 
  throws SAXException ( 
  System.out.println ("\ nFin tag ..."); 
  System.out.println ("\ tNamespace uri:" + uri); 
  System.out.println ("\ tfirst_name:" + localName); 
  System.out.println ("\ tfirst_name with prefix:" + name); 
  ) 

  )
As you can see the class in this example is very silly. I'll just putting messages to stdout as they jumping events.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java and XML"
Thread Thread Starter Forum Replies Last Post
Java Auto Updater wants me to update java everyday in Windows 7 minajoh Windows Software 4 04-09-2013 11:04 PM
Setting Of Java to The Point At Manual Java winni Software Development 4 10-01-2011 09:05 PM
Java Programming using Adventnet SNMP Java API ROCKING_Suhas Software Development 5 17-07-2010 06:52 AM
Link List Example in Java Sample program in Java trickson Software Development 2 04-08-2009 08:23 PM


All times are GMT +5.5. The time now is 04:27 AM.