1

我收到来自 RESTful Web 服务的响应,它可以返回长达 300kb 的字符串。

当我尝试使用 JaxB2 解组时,最多需要 12 秒

有什么我可以做的吗?

public class Convertor{

   JAXBContext responseJaxbContext;

   public Convertor(){
      requestJaxbContext = JAXBContext.newInstance(MyClassResponse.class);   
   }


    public MyClassResponse convertXml(String str) {
         MyClassResponse response = null;   
        try {
             Unmarshaller jaxbUnMarshaller = bookingResponseJaxbContext.createUnmarshaller();
             StringReader reader = new StringReader(str);
             response = (MyClassResponse) jaxbUnMarshaller.unmarshal(reader);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return response;
    }
}

更新:我使用禁用模式验证

jaxbUnMarshaller.setSchema(null);

现在我的解组时间是 5 -7 秒。

4

2 回答 2

0

确保您只实例化 JAXBContext/您的 Convertor 类一次。

在我的经验中,创建 JAXBContext 很昂贵。

编辑:但这不是12s的解释,我猜......

于 2013-06-25T11:31:53.720 回答
0

为了比较,请尝试使用 SAX 解析器解析您的 XML 文档,以计算我们仅花费了多少时间来解析 XML 文档。

import java.io.StringReader;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

public class Demo {

    public static void main(String[] args) throws Exception {
        String xml; // Your XML

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser sp = spf.newSAXParser();
        XMLReader xmlReader = sp.getXMLReader();

        xmlReader.setContentHandler(new DefaultHandler());

        StringReader reader = new StringReader(xml);
        InputSource inputSource = new InputSource(reader);

        // Profile This
        xmlReader.parse(inputSource);
    }

}
于 2013-06-25T13:30:22.617 回答