When Sax parses an XML document, it parses the XML document in a characters method and sometimes doesn't get the data when it is in a messy format.

Source: Internet
Author: User
Tags stringbuffer

First, what is the format of the XML?


First of all, a well-formed chart.

In one that's a poorly formed chart.

As we can see, the difference between the good format and the bad format is that there's a lot more bad than a line break like \ t.

The characters method is called multiple times in a bad format.

So we're on the code

Import Java.io.InputStream;
Import java.util.ArrayList;

Import java.util.List;
Import Javax.xml.parsers.SAXParser;

Import Javax.xml.parsers.SAXParserFactory;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;

Import Org.xml.sax.helpers.DefaultHandler;
	public class Saxparseservice extends DefaultHandler {private list<book> books = null;
	Private book = null;
	
	

	Private String Pretag = null; Public list<book> getbooks (InputStream xmlstream) throws Exception {SAXParserFactory factory = saxparserfactory.
		Newinstance ();
		SAXParser parser = Factory.newsaxparser ();
		Saxparseservice handler = new Saxparseservice ();
		Parser.parse (Xmlstream, handler);
	return Handler.getbooks ();
	Public list<book> Getbooks () {return books;
	@Override public void Startdocument () throws saxexception {books = new arraylist<book> (); } @Override Public VOID startelement (String uri, String localname, String qName, Attributes Attributes) throws Saxexception {if ("book"
			. Equals (QName)) {book = new book ();
		Book.setid (Integer.parseint (attributes.getvalue (0)));
	} Pretag = QName; @Override public void EndElement (string uri, String localname, String qName) throws Saxexception {if (' book '). Equa
			LS (qName)) {books.add (book);
		book = null;
		
	} Pretag = null; 
		@Override public void characters (char[] ch, int start, int length) throws Saxexception {if (Pretag!= null)
			
{String temp = new string (ch,start,length);
			Log.info (Temp.trim (). Equals (""));
			
			
			
			temp = Temp.trim ();
			if ("Name". Equals (Pretag)) {book.setname (temp);
			else if ("Price". Equals (Pretag)) {Book.setprice (Float.parsefloat (temp)); @Override public void Enddocument () throws Saxexception {}}}

This way of writing, in a good format, the data can be properly obtained, only in the bad format of the case filled with a lot of \ n \ t cause the character method was executed several times.

The string temp = new string (ch,start,length);

....

Book.setname (temp);

These two lines of code have been executed several times,

The previous data was flushed out, so there would be no data to be taken,

Results as shown

Title: Thinking in Java result value was flushed off

So now let's look at the correct way of writing, right, is to use the StringBuffer class, using its Append method, no matter how many times the character method is executed, it is append character to the character buffer, the data will not be flushed off.


Add a member variable based on the original code StringBuffer

A new object in the Startelement method can be created.

Finally, the data obtained in the character method is append.

Import Java.io.InputStream;
Import java.util.ArrayList;

Import java.util.List;
Import Javax.xml.parsers.SAXParser;

Import Javax.xml.parsers.SAXParserFactory;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;

Import Org.xml.sax.helpers.DefaultHandler;
	public class Saxparseservice extends DefaultHandler {private list<book> books = null;
	Private book = null;
	
	Private String Pretag = null;
	

	Private StringBuffer SB; Public list<book> getbooks (InputStream xmlstream) throws Exception {SAXParserFactory factory = saxparserfactory.
		Newinstance ();
		SAXParser parser = Factory.newsaxparser ();
		Saxparseservice handler = new Saxparseservice ();
		Parser.parse (Xmlstream, handler);
	return Handler.getbooks ();
	Public list<book> Getbooks () {return books; @Override public void Startdocument () throws saxexception {books = new arraylist<book> (); @Override public void Startelement (string uri, String localname, String qName, Attributes Attributes) throws Saxexcep
		
		tion {sb = new StringBuffer ();
			if ("book". Equals (QName)) {book = new book ();
		Book.setid (Integer.parseint (attributes.getvalue (0)));
	} Pretag = QName; @Override public void EndElement (string uri, String localname, String qName) throws Saxexception {if (' book '). Equa
			LS (qName)) {books.add (book);
		book = null;
		
	} Pretag = null; 
		@Override public void characters (char[] ch, int start, int length) throws Saxexception {if (Pretag!= null)
			
{String temp = new string (ch,start,length);
			Log.info (Temp.trim (). Equals (""));
			
			
			temp = Temp.trim ();
			
			Sb.append (temp);
			if ("Name". Equals (Pretag)) {Book.setname (sb.tostring ());
			else if ("Price". Equals (Pretag)) {Book.setprice (Float.parsefloat (sb.tostring ())); @Override public void Enddocument () throws SAXexception {}} 




The results are as follows



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.