Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAXParser '&' concatenation problem

Tags:

java

xml

sax

I am presently using SAXParser with SAXParserFactory, and I have run into a problem with strings being cuttoff at '&' symbols. For example: "Nation Created Our World & everything in it" becomes "everything in it".

Obviously, I dont want this to happen. In the xml input, the character is properly escaped as &. How can I resolve this?

try{
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader r = sp.getXMLReader();

            //This handles the xml and populates the entries array
            XMLHandler handler = new XMLHandler();


            // register event handlers
            r.setContentHandler(handler);
            String url = "http://foobar.xml";
            r.parse(url);

            return handler.getEntries();
}

I have this in my DefaultHandler class

....
    public void characters( char ch[], int start, int length ){
           String value = new String( ch , start , length );

           if(!value.trim().equals("")) {

               if( currentElement.equalsIgnoreCase("TITLE") ) {
                   tempEntry.setTitle(value);
               }
....
like image 981
Señor Reginold Francis Avatar asked Jul 28 '10 15:07

Señor Reginold Francis


People also ask

How does a SAX parser work?

1.1 The Simple API for XML (SAX) is a push API, an observer pattern, event-driven, serial access the XML file elements sequentially. This SAX parser reads the XML file from start to end, calls one method when it encountered one element, or calls a different method when it found specific text or attribute.

Is SAX parser a push API?

Q 3 - What is a SAX Parser? A - SAX Parser is an event-based parser for xml documents. B - SAX Parser is PUSH API Parser.

How does XML parsing with SAX?

SAX is an API used to parse XML documents. It is based on events generated while reading through the document. Callback methods receive those events. A custom handler contains those callback methods.

What is SAX in HTML?

SAX (Simple API for XML) is an event-driven online algorithm for parsing XML documents, with an API developed by the XML-DEV mailing list. SAX provides a mechanism for reading data from an XML document that is an alternative to that provided by the Document Object Model (DOM).


2 Answers

The SAX API does not guarantee that any given text node will be delivered in one piece. It is permitted to break it up into multiple calls to the characters() method. Your application has to accommodate this possibly, and reassemble the pieces itself.

Incidentally, Nation Created Our World & everything in it is not a valid XML text fragment, it would have to be Nation Created Our World & everything in it. In this case the SAX parser may be breaking it up into Nation Created Our World, & and everything in it, and your app is only remembering the last one.

like image 125
skaffman Avatar answered Nov 15 '22 14:11

skaffman


Thanks skaffman

Implementation,

public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
    // clear tmpValue on start of element
    tmpValue = "";
}

public void characters(char[] ac, int i, int j) throws SAXException {
    tmpValue += new String(ac, i, j);
}
like image 36
Jerald Jose Avatar answered Nov 15 '22 12:11

Jerald Jose