XML Parsing too slow!

心不动则不痛 提交于 2019-12-04 05:02:42

Using a standard XML parser a short message should be parsed in about one milli-second. Using a custom parser you can cut this to about 20 micro-seconds. Any time longer than this is not in the XML parsing

I've runned into the same issue and managed to speed up parser by switching off all validation that DocumentBuilder will do by default:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(false);
factory.setValidating(false);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

// then take a builder via `factory.newDocumentBuilder()` and parse doc with that builder

The parse method takes all the time because it's waiting on the input from the other application. You need to separate the two so you can see what's going on. Read the XML from the other application into a ByteArrayOutputStream, then when that's done, copy the output stream to an input stream (you can use commons-io for that) and feed that to the parser. Then see what is really taking all the time.

One thing that you could optimize is your login process. You could use an LDAP server to do authentication against, LDAP is optimized for reads and you can access it with JNDI.

Mike Dunlavey

What @Nathan said, plus I suggest doing some random pausing while it's taking so much time. I've run into this in the past, and discovered it wasn't the parsing that was taking the time, but the creation and manipulation of data structure as it parsed. You may see something different, but chances are it's a surprise.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!