在java中处理DateTimestamp格式

时间:2016-10-21 13:32:25

标签: java xml date

我发送带有时间戳格式的XML字符串" YYYY-MM-DDTH:M:S"但理想情况下,XSD的日期时间数据类型要求格式为" YYYY-MM-DDTHH:MM:SS"因此处理失败并显示错误消息" XML文件中的时间戳丢失或无效"

由于这种不正确的时间戳格式,在将xml解组为Product Object期间,timestmap被设置为null。因此,当遇到错误的格式时,需要使用正确的时间映射对XML进行预处理。您能否建议使用预期格式自定义/更新时间戳值的最佳方法,并将更新后的xml发回? 示例 如果遇到这个时间戳是" 2016-10-13T2:18:41"它必须改为" 2016-10-13T02:18:41"这适用于小时,分钟和秒。 以下是您的参考代码和要传递的xml字符串值(例如:)

 public Product parseXML(String xml) throws SASException {
    //String xml = "<product><timestamp>2015-10-30T1:41:22.109-04:00</timestamp></product>";

    Product product = null;
    try {
        XMLReader xmlReader = null;
        SAXParserFactory factory = SAXParserFactory.newInstance();

        factory.setNamespaceAware(true);
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        factory.setFeature(ProductConstants.FEATURE_EXTERNAL_GENERAL_ENTITY, false);
        factory.setFeature(ProductConstants.FEATURE_DISALLOW_DOCTYPE_DECLARE, false);

        xmlReader = factory.newSAXParser().getXMLReader();
        InputSource inSrc = new InputSource(new StringReader(xml));

        SAXSource saxSource = new SAXSource(xmlReader, inSrc);
        JAXBContext context = JAXBContext.newInstance(Product.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        product = (Product) unmarshaller.unmarshal(saxSource);

    } catch (ParserConfigurationException | SAXException | JAXBException exception) {
        LOG.error(ProductConstants.ERROR_CODE_XML_UNMARSHALLING_FAILED, exception);
        throw new SASException(ProductConstants.ERROR_CODE_XML_UNMARSHALLING_FAILED, exception);
    }
    LOG.info("Completed product parsing xml: product" + product);
    return product;
}

0 个答案:

没有答案