SAXParser Concatenation Problem

I am currently using SAXParser with SAXParserFactory, and I ran into a problem when strings were cut into '&' characters. For example: "A nation created by our world and everything in it" becomes "everything in it."

Obviously, I do not want this to happen. In xml input, the character is correctly escaped as & . How can i solve 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); } .... 
+4
source share
2 answers

The SAX API does not guarantee that any node text will be delivered in one piece. It is allowed to split it into several calls to the characters() method. Your application should host this, possibly, and assemble the assemblies themselves.

By the way, Nation Created Our World & everything in it not a valid piece of XML text, it must be Nation Created Our World & everything in it Nation Created Our World & everything in it . In this case, the SAX analyzer can split it into Nation Created Our World , & and everything in it , and your application remembers only the last.

+10
source

Thank you scaffman

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); } 
+3
source

All Articles