?
Solved

SAX PARSER - Using TAG for parsing.....

Posted on 2005-03-31
7
Medium Priority
?
320 Views
Last Modified: 2013-11-23
Hi,

Can I use Sax Parser to parse the file using Tag Name, as we do in case of DOM Parser?

If this is possible then how to get all the elements Name and the corresponding values.

A sample code would be very welcome.

TIA

0
Comment
Question by:mickey_lovely
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 

Author Comment

by:mickey_lovely
ID: 13669870
The link that CE gave was for parsing the file.

The thing I wanted is that can I parse the file on the basis of tags, like we do in case of DOM by using getValueByTagName() method.
Can we do similar thing in case of SAX. Can we use the same method or any similar thing?
0
 
LVL 6

Accepted Solution

by:
CodingExperts earned 252 total points
ID: 13669902
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.SAXParser;

public class Flour extends DefaultHandler {

  float amount = 0;

  public void startElement(String namespaceURI, String localName,
                           String qName, Attributes atts) {
    if (namespaceURI.equals("http://recipes.org") && localName.equals("ingredient")) {
       String n = atts.getValue("","name");
       if (n.equals("flour")) {
         String a = atts.getValue("","amount"); // assume 'amount' exists
         amount = amount + Float.valueOf(a).floatValue();
       }
    }
  }

  public static void main(String[] args) {
    Flour f = new Flour();
    SAXParser p = new SAXParser();
    p.setContentHandler(f);
    try { p.parse(args[0]); }
    catch (Exception e) {e.printStackTrace();}
    System.out.println(f.amount);
  }
}

-CE
0
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 248 total points
ID: 13670745
Look at: http://www.saxproject.org/quickstart.html
it is a short and quick tutorial to SAX that uses your needs (extract / printout) the name of elements and content
as its example (source code is embeded in that page)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question