简体   繁体   中英

How to get entire xml as string from stream using SAX parser?

i know you can convert InputStream to string using something like :

 public static String convertStreamToString(ServletInputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

Which is fine, however my input stream is an xml I parse using SAX Parser, and I was wondering if there is an faster way to get the XML as string, since the parser already loops through the stream.

I can get it to work by concating the string in all the events,but I was wondering if there's a faster/built in way to do so, since this code is really performance-sensitive

If you have to use SAX, there doesn't seem to be a built-in way, see Echoing an XML File with the SAX Parser . Or consider using StAX if you can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM