The server side has XML flow, in order to parse, the first method is to build a StringBuilder, in the OnMessage method is constantly append, eventually forming a complete XML file string (which contains multiple XML). Finally, use Documentbuilder to parse. This practice can be maintained when the amount of data is small, and when a large amount of XML comes in, oom occurs.
The improved method uses the edge to receive data, the side-resolved asynchronous way, so that the parsed part can be discarded without continuing to occupy memory.
Getting ready to work requires a familiar API,
com.sun.jersey.api.client.Client : A client that is used to build a request for a restful request, which can be set to the requested parameters, such as whether it is called asynchronously, the header information requested, and so on.
java.util.concurrent.future<inputstream>: Receives the result returned by the asynchronous request, can be used Future.isdone (), future.iscancelled () to determine Execution status such as "whether to return" or "whether to cancel".
1 future<inputstream> future = client.create ()2 . Asyncresource ( Requestbean.geturl ())3 . Entity (Requestbean.getrequestbody (), "Application/json") 4 . Post (InputStream. Class);
com.google.common.base.function<node,void>: A custom callback method that handles the data that is obtained when a return is completed. node is the basic element of XML, void means that no return value is defined for the return.
1 New Function<node, void>() {2public Void apply (Node tradenode) {3 //Process node and customized logic etc. 4 5
javax.xml.stream.XMLOutputFactory: Build factory classes to instantiate Xmleventwriters and Xmlstreamwriters.
Javax.xml.parsers.DocumentBuilder:Create the DOM to parse the XML data source, which can be inputstreams, Files, URLs, and sax inputsources.
Javax.xml.stream.XMLEventReader:This class is the top-level interface for parsing XML events. It provides the ability to view the next event and to return configuration information in the properties interface. XML data can be facilitated through a while (Event.hasnext ()).
Javax.xml.stream.XMLEventWriter:An interface that generates an XML file that does not validate the legitimacy of the XML format
javax.xml.namespace.QName.QName (String NamespaceURI, string localpart): Building an XML node
1 Public voidHandleresponsestream (InputStream stream, Function<node, void> callback)throwsexception{2 3Xmloutputfactory OUTFAC =xmloutputfactory.newinstance ();4Documentbuilder Dombuilder =documentbuilderfactory.newinstance (). Newdocumentbuilder ();5Xmleventreader r =xmlinputfactory.newinstance (). Createxmleventreader (stream);6Xmleventwriter XEW =NULL;7Domresult Domresult =NULL;8QName root =NewQName ("http://www.namespace.com", "Tagstart");9 Ten while(R.hasnext ()) { OneXmlevent e =r.nextevent (); A if(E.isstartelement () &&Root.equals (((startelement) e). GetName ())) { -Domresult =NewDomresult (Dombuilder.newdocument ()); -XEW =Outfac.createxmleventwriter (domresult); the } - if(Xew! =NULL) { - Xew.add (e); - } + if(E.isendelement () &&Root.equals (((EndElement) e). GetName ())) { - xew.close (); + callback.apply (Domresult.getnode (). Getfirstchild ()); AXEW =NULL; at } - } -}
Parse the XML file stream returned from the server side