简体   繁体   English

如何使用SAX解析器从流中获取整个xml作为字符串?

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

i know you can convert InputStream to string using something like : 我知道您可以使用类似以下内容将InputStream转换为字符串:

 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. 很好,但是我的输入流是我使用SAX Parser解析的xml,我想知道是否有更快的方法将XML作为字符串获取,因为解析器已经在该流中循环。

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 . 如果必须使用SAX,似乎没有内置的方法,请参阅使用SAX Parser回显XML文件 Or consider using StAX if you can. 或者,如果可以的话,考虑使用StAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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