• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does the SAXParser give different result ?

 
Chart Krobtragolchai
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use NetBeans to create the simple SAX code to parse the Train.xml below :

<?xml version="1.0" encoding="UTF-8"?>
<train xmlns:myPrefix="http://example.com">
<car type="Engine">
<color>Black</color>
<weight>512 tons</weight>
<length>60 feet</length>
<occupants>3</occupants>
</car>
<car type="Baggage">
<color>Green</color>
<weight>80 tons</weight>
<length>40 feet</length>
<occupants>0</occupants>
</car>
<car type="Dining">
<color>Green and Yellow</color>
<weight>50 tons</weight>
<length>50 feet</length>
<occupants>18</occupants>
</car>
<car type="Passenger">
<color>Green and Yellow </color>
<weight>40 tons</weight>
<length>60 feet</length>
<occupants>23</occupants>
</car>
<car type="Pullman">
<color>Green and Yellow</color>
<weight>50 tons</weight>
<length>60 feet</length>
<occupants>23</occupants>
</car>
<car type="Caboose">
<color>Red</color>
<weight>90 tons</weight>
<length>30 feet</length>
<occupants>4</occupants>
</car>
</train>


import java.io.File;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class TrainReader extends DefaultHandler{

private boolean isColor;
private String trainCarType = "";
private StringBuffer trainCarColor = new StringBuffer();

/** Creates a new instance of TrainReader */
public TrainReader() {
}

public static void main(String[] args)throws Exception{
System.out.println("Running train reader...");
TrainReader readerObj = new TrainReader();
readerObj.read(args[0]);
}

public void read(String fileName)throws Exception{
//Code 1
SAXParserFactory spfactory = SAXParserFactory.newInstance();
SAXParser saxParser = spfactory.newSAXParser();
spfactory.setValidating(true);
spfactory.setNamespaceAware(true);
saxParser.parse(new File(fileName),this);

//Code 2
//XMLReader reader = XMLReaderFactory.createXMLReader();
//reader.setContentHandler(this);
//reader.parse(fileName);
}

public void startDocument() throws SAXException{
System.out.println("Start of the train");
}

public void endDocument() throws SAXException{
System.out.println("End of the train");
}

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

if (localName.equals("car")){
if (attributes != null){
trainCarType = attributes.getValue("type");
}
}

if (localName.equals("color")){
trainCarColor.setLength(0);
isColor = true;
}else{
isColor = false;
}

}

public void characters(char[] ch, int start, int length) throws SAXException {
if(isColor){
trainCarColor.append(ch,start,length);
}
}

public void endElement(String uri, String localName, String qName) throws SAXException {
if(isColor){
System.out.println("The color of the " + trainCarType + " car is" +
trainCarColor.toString());
}
isColor = false;
}

public void startPrefixMapping(String prefix, String uri) throws SAXException {
System.out.println("Start of Prefix Mapping");
System.out.println("The prefix is " + prefix);
System.out.println("The uri is " + uri);
}

public void endPrefixMapping(String prefix) throws SAXException {
System.out.println("End of Prefix Mapping");
System.out.println("The prefix is " + prefix);
}
}

The problem is why it give different result between Code 1 and Code 2 in read(String fileName) method.

When use code 1, the result is :
Running train reader...
Start of the train
End of the train


When use code 2, the result is :
Running train reader...
Start of the train
Start of Prefix Mapping
The prefix is myPrefix
The uri is http://example.com
The color of the Engine car isBlack
The color of the Baggage car isGreen
The color of the Dining car isGreen and Yellow
The color of the Passenger car isGreen and Yellow
The color of the Pullman car isGreen and Yellow
The color of the Caboose car isRed
End of Prefix Mapping
The prefix is myPrefix
End of the train

In code 1, the localName is always blank. I try to change the below value in all combination- true-true, true-false,flase-flase, flase-true.
spfactory.setValidating(true);
spfactory.setNamespaceAware(true);
 
Chart Krobtragolchai
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Continue qustion :

//code 3
SAXParserFactory spfactory = SAXParserFactory.newInstance();
SAXParser saxParser = spfactory.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
reader.setContentHandler(this);
reader.parse(fileName);

I think that the result of code 3 and code 2 should be the same but it gives the result like code 1.

What happen ?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

SAXParserFactory spfactory = SAXParserFactory.newInstance();
spfactory.setNamespaceAware(true);
SAXParser saxParser = spfactory.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
reader.setContentHandler(this);
reader.parse(fileName);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic