Results 1 to 4 of 4

Thread: Java and XML

  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Setting Of Java to The Point At Manual Java
    By winni in forum Software Development
    Replies: 4
    Last Post: 10-01-2011, 09:05 PM
  3. Java Programming using Adventnet SNMP Java API
    By ROCKING_Suhas in forum Software Development
    Replies: 5
    Last Post: 17-07-2010, 06:52 AM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •