XML Parsing too slow!

2020-07-06 08:31发布

I wrote a java app to communicate with a web application using XML. After deployment, I found out it takes too long to parse the XML generated by the web application.

For example, it takes about 2 minutes to login; the login information is included in the url. The web application does its processing and responds to the Java app whether the login was successful using XML returned.

I used the standard java DOM parsing.

Is there a way I can optimize this process so that activities can be faster?

4条回答
够拽才男人
2楼-- · 2020-07-06 09:16

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.

查看更多
干净又极端
3楼-- · 2020-07-06 09:17

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
查看更多
对你真心纯属浪费
4楼-- · 2020-07-06 09:23

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.

查看更多
叼着烟拽天下
5楼-- · 2020-07-06 09:33

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

查看更多
登录 后发表回答