Results 1 to 2 of 2
Thread: Java xml parsing help
- 11-07-2012, 11:50 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
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)
*/
@Override
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.
*/
@Override
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
accumulator.setLength(0);
}
/**
* 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")){
//right??//accumulator.setFeatures(temp);
}
// Reset the accumulator because we have seen the value
accumulator.setLength(0);
}
/**
* 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());
}
@Test
public void testIdFirst() {
assertEquals("Law", first.getId());
}
@Test
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.
thanks
- 11-09-2012, 05:05 PM #2
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
-
Parsing Java Date
By Sno in forum New To JavaReplies: 3Last Post: 11-24-2010, 04:06 PM -
parsing XML in JAVA
By krishkill in forum Advanced JavaReplies: 5Last Post: 05-30-2010, 07:59 PM -
Parsing Problem in Java
By vidya in forum Advanced JavaReplies: 4Last Post: 02-01-2010, 03:48 PM -
E:\IT 215 Java Programming\Inventory.java:211: reached end of file while parsing
By tlouvierre in forum New To JavaReplies: 1Last Post: 05-31-2009, 06:48 PM -
Parsing URL in Java
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks