Results 1 to 2 of 2
  1. #1
    linear is offline Member
    Join Date
    Nov 2012
    Rep Power

    Default Java xml parsing help

    package ca.ubc.cpsc210.sustainabilityapp.model.parser;

    import java.util.List;

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

    import ca.ubc.cpsc210.sustainabilityapp.model.Feature;
    import ca.ubc.cpsc210.sustainabilityapp.model.PointOfInte rest;

    public class MapInfoParser extends DefaultHandler {

    private List<PointOfInterest> reg;
    private String temp;//added
    StringBuffer accumulator;
    public MapInfoParser(List<PointOfInterest> reg) {

    this.reg = reg;
    // Remember information being parsed

    * Called at the start of the document (as the name suggests)
    public void startDocument() {
    // Print out a message to let you know something is happening. This is
    // just to help you trace the program executing.
    System.out.println("Start Document!");
    //test Feature f = Feature.parseFeature("Biofuel");
    // Use accumulator to remember information parsed. Just initialize for
    // now.
    accumulator = new StringBuffer();

    * Called when the parsing of an element starts. (e.g., <book>)
    * Lookup documentation to learn meanings of parameters.
    public void startElement(String namespaceURI, String localName,
    String qName, Attributes atts) {
    System.out.println("StartElement: " + qName);
    // What are we parsing?
    if (qName.toLowerCase().equals("poi")) {
    // information about the location
    System.out.println(atts.getValue("address"));//what is this??

    // Let's start the accumulator
    // to remember the value that gets parsed

    * Called for values of elements
    * Lookup documentation to learn meanings of parameters.
    public void characters(char[] temp, int start, int length) {
    // Remember the value parsed
    accumulator.append(temp, start, length);

    * Called when the end of an element is seen. (e.g., </title>)
    * Lookup documentation to learn meanings of parameters.
    public void endElement(String uri, String localName, String qName) {
    // Print out that we have seen the end of an element
    System.out.println("EndElement: " + qName + " value: " + accumulator.toString().trim());
    if (qName.toLowerCase().equals("address")) {
    reg.add("address");//not sure
    // Do something with the title that we have just seen
    }else if (qName.equals("Features")){

    // Reset the accumulator because we have seen the value

    * Called when the end of the document is reached
    public void endDocument() {
    // Just let the user know as something to do
    System.out.println("End Document!");


    __________________________________________________ ________________
    public void testNumPOIs() {
    assertEquals(7, reg.size());

    public void testIdFirst() {
    assertEquals("Law", first.getId());

    public void testIdLast() {
    assertEquals("LSC", last.getId());
    __________________________________________________ _
    Hi guys,

    I need some help with parsing XML files using sax, the first part of the code is what i've written but i'm having trouble passing some tests(the second part of the code, after the line). I'm having trouble understanding exactly how parsing works, i've read up on it but just seem to be getting more confused.


  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

    Default Re: Java xml parsing help

    Hello and welcome! Please use [code][/code] tags when posting code so we can easily read it!

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    Could you also tell use exactly where the trouble is, and what errors you are getting?

Similar Threads

  1. Parsing Java Date
    By Sno in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 04:06 PM
  2. parsing XML in JAVA
    By krishkill in forum Advanced Java
    Replies: 5
    Last Post: 05-30-2010, 07:59 PM
  3. Parsing Problem in Java
    By vidya in forum Advanced Java
    Replies: 4
    Last Post: 02-01-2010, 03:48 PM
  4. Replies: 1
    Last Post: 05-31-2009, 06:48 PM
  5. Parsing URL in Java
    By Java Tip in forum
    Replies: 0
    Last Post: 04-07-2008, 08:14 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